Join in Java thread

Source: Internet
Author: User

I planProgramChanged to multi-thread. Several items are found in the test to reflect the changes. first look at them.CodeRight:

 

Class testmain extends thread {</P> <p> Public testmain () {<br/> super ("current thread 1 "); <br/>}< br/> Public void run () {</P> <p> for (INT I = 0; I <10; I ++) {<br/> system. out. println (thread. currentthread (). getname () + ":" + I); <br/> try {<br/> thread. sleep (100); <br/>} catch (interruptedexception e) {<br/> E. printstacktrace (); <br/>}</P> <p> class testmain1 extends thread {</ p> <p> Public testmain1 () {<br/> super ("current thread 2"); <br/>}< br/> Public void run () {</P> <p> for (INT I = 0; I <10; I ++) {<br/> system. out. println (thread. currentthread (). getname () + ":" + I); <br/> try {<br/> thread. sleep (8000); <br/>} catch (interruptedexception e) {<br/> E. printstacktrace (); <br/>}</P> <p> public class threadtest {</P> <p> Public static void main (string [] ARGs) throws exception {<br/> testmain T = new testmain (); <br/> testmain1 T1 = new testmain1 (); <br/> T. start (); <br/> t1.start (); <br/> T. join (); <br/> t1.join (); <br/> system. out. println (111); <br/>}</P> <p>}

 

In the previous code, if there is no join, the main thread continues to execute after two sub-threads are scheduled. If there is a join, then, we will wait until the sub-thread completes execution and then execute the print "111" of the main thread ". No matter whether there is a join, the program will wait until the sub-thread ends, instead of the end of the main thread after the end of the non-pipe thread.

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.