C #. Details about thread. Join () in various Java versions

Source: Internet
Author: User

The function of the join method is to convert the asynchronous thread into synchronous execution. That is to say, after the start method of the thread instance is called, this method will return immediately. If a value calculated by this thread needs to be used after the start method is called, you must use the join method. If the join method is not used, it cannot be guaranteed that the thread will be executed completely when a statement following the start method is executed. After the join method is used, the program will not execute until the thread exits. The following code demonstrates the usage of multi-thread. Join in two languages.

Java version:

Package com.cn.net; public class test {/*** @ Param ARGs * @ throws interruptedexception */public static void main (string [] ARGs) throws interruptedexception {// todo auto-generated method stub // thread a final thread threada = new thread (New runnable () {@ overridepublic void run () {for (INT I = 0; I <10; I ++) {system. out. println ("A:"); if (I = 9) {break;} Try {thread. sleep (1000);} catch (interruptedexception e) {// todo auto-generated catch blocke. printstacktrace () ;}}}); thread threadb = new thread (New runnable () {@ overridepublic void run () {for (INT I = 0; I <5; I ++) {system. out. println ("B:"); if (I = 4) {break;} Try {thread. sleep (1000);} catch (interruptedexception e) {// todo auto-generated catch blocke. printstacktrace () ;}// try {// threada. join (); // insert thread a //} catch (interruptedexception e) {// todo auto-generated Catch Block // E. printstacktrace (); //} For (INT I = 0; I <5; I ++) {system. out. println ("C:"); if (I = 4) {break;} Try {thread. sleep (1000);} catch (interruptedexception e) {// todo auto-generated catch blocke. printstacktrace () ;}}}); threada. start (); threadb. start ();}
}

C # version:

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading; namespace mutiprocess {class program {static void main (string [] ARGs) {// thread a thread threada = new thread (delegate () {for (INT I = 0; I <10; I ++) {console. writeline ("A:"); if (I = 9) {break;} thread. sleep (1000) ;}}); // thread B thread threadb = new thread (delegate () {for (INT I = 0; I <5; I ++) {console. writeline ("B:"); if (I = 4) {break;} thread. sleep (1000);} threada. join (); // insert thread a for (INT I = 0; I <5; I ++) {console. writeline ("C:"); if (I = 4) {break;} thread. sleep (1000) ;}}); threada. start (); threadb. start (); // a B appears alternately in the first 5 seconds, then a appears 5 times, and C appears 5 times. // If threada. Join () is commented out, the result is that AB appears in the first 5 seconds and AC appears in the next 5 seconds. // Here I will not be arrogant. In other words, if thread a is joined in thread B, only the execution of thread a is finished can the remaining code in thread B be continued. // Join is actually to convert the asynchronous execution thread into synchronous execution. }}}

The execution result of version 2 is (CPU determined ):

A:
B:
A:
B:
A:
B:
B:
A:
B:
A:
A:
A:
A:
A:
A:
C:
C:
C:
C:
C:

When threada. Join () is commented out, the following rule is displayed:

A:
B:
A:
B:
A:
B:
A:
B:
A:
B:
C:
A:
C:
A:
C:
A:
C:
A:
C:
A:

 

More explanations and examples of Multithreading

Http://developer.51cto.com/art/200906/132331.htm

Http://www.cnblogs.com/qinpengming/archive/2012/05/08/2490665.html

 

 

 

 

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.