JavaScript writes multithreaded Code reference Concurrent.Thread.js (RPM)

Source: Internet
Author: User
Tags script tag

This is a very simple feature implementation:

<script type= "Text/javascript" src= "Concurrent.Thread.js" ></script> <script type= "Text/javascript" >     Concurrent.Thread.create (function () {         var i = 0;         while (1) {             Document.body.innerHTML + = i++ + "<br>";         }     }); </script>

  

The execution of this program will show the numbers starting from 0 sequentially, they appear one after another, you can scroll to see it. Now let's take a closer look at the code, and he's using the while (1) condition to create a loop that doesn't stop, and typically, a JavaScript program that constantly uses one and only one thread will cause the browser to look like it freezes, and it will not allow you to roll the screen. So why does the program above allow you to do this? The key is that the Concurrent.Thread.create () statement above the while (1), which is a method provided by this library, can create a new thread. The function passed as a parameter is executed in this new thread, so let's fine-tune the program as follows:

<script type="Text/javascript"Src="Concurrent.Thread.js"></script> <script type="Text/javascript">function f (i) { while(1) {Document.body.innerHTML+ = i++ +"<br>"; }} Concurrent.Thread.create (F,0); Concurrent.Thread.create (F,100000); </script>

In this program there is a new function f () can repeat the number, it is defined in the beginning of the program section, and then the F () for the parameter call two times the Create () method, the second parameter passed to the Create () method will be passed to F () without modification. To execute this program, you will see some decimal numbers starting with 0, followed by a number of large numbers starting from 100,000, followed by a decimal order. You can observe that the program is alternately displaying decimals and large numbers, which means that two threads are running at the same time.

Let me show you another use of concurrent.thread. The example above calls the Create () method to make a new thread. It is also possible to do this without invoking any APIs in the library. For example, the previous example could be written like this:

<script type="text/javascript" src="Concurrent.Thread.js" ></script> <script type="text/x-script.multithreaded-js">     var 1 ;       while 1 ) {         "<br>";     

Within the script tag, it's easy to write an infinite loop with JavaScript. You should notice the type attribute inside the tag, there is a very strange value (text/x-script.multithreaded-js), if this attribute is placed inside the script tag, Then Concurrent.thread will execute the program between the tags within a new thread. You should keep in mind that, as in this example, the Concurrent.thread library must be included.

With Concurrent.thread, it's possible to switch between the execution environment threads, even if your program is long and consistent. We can briefly discuss how to perform this operation. In short, code conversions are required. Roughly speaking, you first convert the function passed to create () to a string, and then rewrite it until it can be executed in batches. These programs can then be progressively executed in accordance with the scheduler. The scheduler is responsible for coordinating multi-threading, in other words, it can make adjustments at the appropriate time so that every modified function gets the same chance to run. Concurrent.thread actually did not create a new thread, only simulating a multithreaded environment on the basis of the original single thread.

Although the converted function appears to be running in a different thread, there is actually only one thread doing all of this. Performing synchronous communication within the converted function will still cause the browser to freeze, and you may think that the previous problems have not been solved at all. But you don't have to worry, Concurrent.thread provides a custom communication library that is implemented using JavaScript asynchronous communication, which is designed to allow other threads to run when a thread waits for a response from the server. This communication is in stock under Concurrent.Thread.Http. Its usage is as follows:

<script type="Text/javascript"Src="Concurrent.Thread.js"></script> <script type="Text/x-script.multithreaded-js">varreq = Concurrent.Thread.Http.Get(URL, ["Accept","*"]); if(Req.status = = $) {alert (req.responsetext); } Else{alert (req.statustext); } </script>

The Get () method, as its name implies, can get the contents of the specified URL through the Get method of HTTP, which takes the destination URL as the first parameter and an array representing the HTTP request header as an optional second parameter. The Get () method interacts with the server and returns a XMLHttpRequest object as the return value when the response from the server is obtained. When the Get () method returns, the server response has been received, so there is no need to use the callback function to receive the result. Naturally, there is no need to worry about when the program waits for the server to respond when the browser freezes. In addition, there is a post () method that can be used to send data to the server:

<script type="text/javascript" src="Concurrent.Thread.js" ></script> <script type="text/x-script.multithreaded-js">     var " Key1=val1&key2=val2 " );      </script>

The post () method takes the destination URL as the first parameter, and the content to be sent as the second parameter. Like the Get () method, you can also use the request header as an optional third parameter.

If you use this communication library to implement the Getarticle () method in the first example, you will soon be able to apply the simple method of writing Getarticlewithcache (), Backgroundload () in the beginning of the article. and other functions that call the Getarticle () method. Even if that version of Backgroundload () is reading the article data, as usual there is another thread that can respond to user requests and therefore the browser will not freeze.

JavaScript writes multithreaded Code reference Concurrent.Thread.js (RPM)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.