There are three threads T1 T2 T3, how to ensure that they are executed sequentially-reproduced

Source: Internet
Author: User

T3 is executed first, in the T3 run, call T2.join, let the T2 execution complete and then execute T3

In T2 run, call T1.join and let the T1 execute after the execution of the T2

 Public classTest {//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) {final Thread T1=NewThread (NewRunnable () {@Override Public voidrun () {System. out. println ("T1");}}); Final Thread T2=NewThread (NewRunnable () {@Override Public voidrun () {Try {//referencing the T1 thread, waiting for the T1 thread to finish executingt1.join ();} Catch(interruptedexception e) {e.printstacktrace ();} System. out. println ("T2");}}); Thread T3=NewThread (NewRunnable () {@Override Public voidrun () {Try {//referencing the T2 thread, waiting for the T2 thread to finish executingt2.join ();} Catch(interruptedexception e) {e.printstacktrace ();} System. out. println ("T3");}}); T3.start (); T2.start (); T1.start ();//these three threads start with no sequencing}}

There are three threads T1 T2 T3, how to ensure that they are executed sequentially-reproduced

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.