Join Usages in Java threads

Source: Internet
Author: User

The JDK is interpreted as Waits for the thread to die. Wait for this thread to finish before the next thread can run.

Instance requirements:

Now there are T1, T2, T3 three threads, how do you ensure that T2 execution after T1 execution, T3 after the T2 execution

Implementation code:

 Packagecom.st.lesson02; Public classTest01 {//1. Now there are T1, T2, T3 three threads, how do you ensure that T2 after T1 execution, T3 after the execution of T2 executed     Public Static voidMain (string[] args)throwsinterruptedexception {Thread Th1=NewThread01 (); Thread Th2=NewThread02 (); Thread Th3=NewThread03 ();        Th1.start ();        Th1.join (); System.out.println ("Thread01 Run End ... ");        Th2.start ();        Th2.join (); System.out.println ("Thread02 Run End ... ");        Th3.start ();        Th3.join (); System.out.println ("Thread03 Run End ... "); System.out.println ("------Main function-------"); }}classThread01extendsthread{ Public voidrun () {System.out.println ("Thread01...running ..."); Try{Thread.Sleep (2000); } Catch(interruptedexception e) {e.printstacktrace (); }    }}classThread02extendsthread{ Public voidrun () {System.out.println ("Thread02...running ..."); Try{Thread.Sleep (500); } Catch(interruptedexception e) {e.printstacktrace (); }            }}classThread03extendsthread{ Public voidrun () {System.out.println ("Thread03...running ..."); Try{Thread.Sleep (1000); } Catch(interruptedexception e) {e.printstacktrace (); }            }}

Run:

Join Usages in Java threads

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.