Analysis of the sequence of thread. Join () before and after start ()

Source: Internet
Author: User

In multithreading, the join () method is often used. What is join?

The join () action of the thread class can combine two threads that are alternating and strict in order. For example, if the join () Action of thread a is called in thread B, thread a will continue to enforce thread B until thread a ends.

The Code is as follows:

Package com.cn. process; public class test {/*** @ Param ARGs */public class Hello implements runnable {@ overridepublic void run () {// todo auto-generated method stub for (INT I = 0; I <3; I ++) {system. out. println (thread. currentthread (). getname () + I) ;}} public static void main (string [] ARGs) throws interruptedexception {string threadname = thread. currentthread (). getname (); test = new test (); hello He = test. new Hello (); thread demo = new thread (he, "Thread"); // The second parameter is the thread name (custom) demo. start (); // call the start method, but call this method only to prepare the thread and not to start the demo immediately. join (); // force-Execute demo. The main thread can continue to run the current main thread only after the demo thread is executed. This is equivalent to adding a subthread of demo to the main thread, convert the asynchronous execution thread to synchronous execution // demo. the results displayed by the START (); // join () method before and after start () are obviously different. It can be proved that the join () method can be correctly executed only after start () is started () for (INT I = 0; I <50; ++ I) {system. out. println (threadname + "thread execution -->" + I );}}}

The execution result is:

Thread 0
Thread 1
Thread 2
Main thread execution --> 0
Main thread execution --> 1
Main thread execution --> 2
Main thread execution --> 3
Main thread execution --> 4
Main thread execution --> 5
Main thread execution --> 6
Main thread execution --> 7
Main thread execution --> 8
Main thread execution --> 9
Main thread execution --> 10
Main thread execution --> 11
Main thread execution --> 12
Main thread execution --> 13
Main thread execution --> 14
Main thread execution --> 15
Main thread execution --> 16
Main thread execution --> 17
Main thread execution --> 18
Main thread execution --> 19
Main thread execution --> 20
Main thread execution --> 21
Main thread execution --> 22
Main thread execution --> 23
Main thread execution --> 24
Main thread execution --> 25
Main thread execution --> 26
Main thread execution --> 27
Main thread execution --> 28
Main thread execution --> 29
Main thread execution --> 30
Main thread execution --> 31
Main thread execution --> 32
Main thread execution --> 33
Main thread execution --> 34
Main thread execution --> 35
Main thread execution --> 36
Main thread execution --> 37
Main thread execution --> 38
Main thread execution --> 39
Main thread execution --> 40
Main thread execution --> 41
Main thread execution --> 42
Main thread execution --> 43
Main thread execution --> 44
Main thread execution --> 45
Main thread execution --> 46
Main thread execution --> 47
Main thread execution --> 48
Main thread execution --> 49

OK successfully plays the role of join!

When I comment out the previous start,

The following is a code snippet:

// Demo. Start (); // call the start method, but only prepare the thread and not start it immediately.
Demo. join (); // force-Execute demo. The main thread can continue to run the current main thread only after the demo thread is executed. This is equivalent to adding a subthread of demo to the main thread, convert asynchronous execution threads to synchronous execution
Demo. the results displayed by the START (); // join () method before and after start () are obviously different. It can be proved that only when start () is started can join () be executed, that is, when join () in the previous step, the meaning is lost.

The execution result may be (CPU-related ):

Thread 0
Main thread execution --> 0
Main thread execution --> 1
Main thread execution --> 2
Main thread execution --> 3
Main thread execution --> 4
Main thread execution --> 5
Main thread execution --> 6
Main thread execution --> 7
Main thread execution --> 8
Main thread execution --> 9
Thread 1
Thread 2
Main thread execution --> 10
Main thread execution --> 11
Main thread execution --> 12
Main thread execution --> 13
Main thread execution --> 14
Main thread execution --> 15
Main thread execution --> 16
Main thread execution --> 17
Main thread execution --> 18
Main thread execution --> 19
Main thread execution --> 20
Main thread execution --> 21
Main thread execution --> 22
Main thread execution --> 23
Main thread execution --> 24
Main thread execution --> 25
Main thread execution --> 26
Main thread execution --> 27
Main thread execution --> 28
Main thread execution --> 29
Main thread execution --> 30
Main thread execution --> 31
Main thread execution --> 32
Main thread execution --> 33
Main thread execution --> 34
Main thread execution --> 35
Main thread execution --> 36
Main thread execution --> 37
Main thread execution --> 38
Main thread execution --> 39
Main thread execution --> 40
Main thread execution --> 41
Main thread execution --> 42
Main thread execution --> 43
Main thread execution --> 44
Main thread execution --> 45
Main thread execution --> 46
Main thread execution --> 47
Main thread execution --> 48
Main thread execution --> 49

This shows whether the order of join () takes effect!

 

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.