Start with the company's first pen test.
1 Packagetest;2 3 Public classTestImplementsRunnable {4 5 Public inti = 0;6 7 @Override8 Public voidrun () {9 Try {TenThread.Sleep (1000); One}Catch(interruptedexception e) { A //TODO auto-generated Catch block - e.printstacktrace (); - } thei = 10; - } - - Public Static voidMain (string[] args) { + Try { -Test T =NewTest (); +Thread th =NewThread (t); A Th.start (); at Th.join (); - System.out.println (T.I); -}Catch(Exception ex) { - - } - in } -}
Ask 23 lines of code How to write, to let 24 lines print out 10?
Many written examinations will choose t.wait () or th.wait ()!
The interview asked him why, he specifically also said not clear, the feeling is to see this wait method, but the meaning of the wait method is indeed smattering.
What does wait mean? For example, I want this thread to give up the current object lock, so it's straightforward to let other objects get into the synchronization block.
1 Packagetest;2 3 Public classTestImplementsRunnable {4 5 PublicObject i =NewObject ();6 7 @Override8 Public voidrun () {9 synchronized(i) {TenSystem.out.println (Thread.CurrentThread (). GetName () + "enter"); One //i.notify (); A Try { - i.wait (); -}Catch(interruptedexception e) { the //TODO auto-generated Catch block - e.printstacktrace (); - } -System.out.println (Thread.CurrentThread (). GetName () + "out"); + } - } + A Public Static voidMain (string[] args) { at Try { -Test T =NewTest (); -Thread Th1 =NewThread (t); -Thread Th2 =NewThread (t); - Th1.start (); - Th2.start (); in - to}Catch(Exception ex) { + - } the * } $}
As in the example above, you will see the output
Thread-0enter
Thread-1enter
Won't see
Thread-1out
Thread-0out
Because Thread-0 first obtains the object I lock, then runs to 13 lines, releases the lock,
At this point, Thread-1 obtains the object I lock, enters the synchronization code block, and then runs the same 13 lines, releasing the lock.
This time there are two threads Thread-0 and Thread-1 waiting to get the object I lock, because the code does not call I.notifyall (), so this program will never quit.
But if you open the comment 11 line, you'll see the result
Thread-0enter
Thread-1enter
Thread-0out
Because Thread-0 first obtains the object I lock, then runs to 13 lines, releases the lock,
at this time Thread-1 obtained the object I lock, entered the synchronization code block, runs to 11 lines, I. Notify (),
So this means that another thread waiting for the I lock can wake up, and once I (THREAD-1) releases the lock (13 lines call Wait ()), then Thread-0 can get the I lock to continue execution.
This program does not notify after Thread-1 release I lock (Wait ()), so you never see thread-1out
Back to this topic, we mean that the main thread waits for all the child threads to execute before executing. Moreover, there is no lock object in the written question. Not to mention wait ().
Therefore, the th.join () should be used here; the thread.join () method blocks the main thread from continuing down execution.
1 Public classTestthreadextendsThread2 { 3 PrivateCountdownlatch Countdownlatch; 4 5 PublicTestthread (countdownlatch countdownlatch)6 { 7 This. Countdownlatch =Countdownlatch; 8 } 9 Ten Public voidRun () One { ASystem.out.println ( This. GetName () + "Child thread Start"); - Try - { the //Child thread sleeps for five seconds -Thread.Sleep (5000); - } - Catch(interruptedexception e) + { - E.printstacktrace (); + } A atSystem.out.println ( This. GetName () + "Child Thread End"); - - //countdown timer minus 1 . - Countdownlatch.countdown (); - } -}
1 Public classMain2 {3 Public Static voidMain (string[] args)4 {5 LongStart =System.currenttimemillis ();6 7 //Create a countdown counter with an initial value of 58Countdownlatch Countdownlatch =NewCountdownlatch (5);9 for(inti = 0; I < 5; i++)Ten { OneThread thread =NewTestthread (countdownlatch); A Thread.Start (); - } - the Try - { - //blocks the current thread until the countdown counter is down to 0 - countdownlatch.await (); + } - Catch(interruptedexception e) + { A e.printstacktrace (); at } - - LongEnd =System.currenttimemillis (); -SYSTEM.OUT.PRINTLN ("Child Thread Execution Duration:" + (End-start)); - } -}
Java Multithreading--Let the main thread wait for all child threads to complete join