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