I didn't have time to write this article yesterday. I fill it today. I only explain the two methods of WAIT and Notify. I discussed Sleep and Join, which is more than Wait and Notify simple. Http: // Blog.9cbs.net/treeroot/archive/2004/11/10/175508.aspx
Sleep: THREAD static method, current thread sleep for a while, time to restore it can run, time is not necessarily executed, but also compete for CPU.
JOIN: This method is actually a special wait. WAIT method usually requires someone NOTIFY (of course, it can also set up timeout), but the Join method does not require someone NOTIFY, and I have been waiting until this thread is time to tell those temporary. People waiting for it: You come in!)
I am not very excited, or two people are using a bathroom. This is not brushing, change the bath, you can't take a bath! Even if you can, you can't. , B needs to wait.
The first case: B is very smart, a bath may take 20 minutes to 1 hour, I will sleep for 10 minutes, don't sleep, don't sleep, sleep for 10 minutes, one more 10 minutes.
Class syn {public static void main (string [] args) throws exception {thread a = new bath (); a.start (); // b int Time = 0; while (a.isalive ()) {thread.sleep (10000); TIME = 10; System.out.Println ("b Has WaITED" Time "Minutes");} system.out.println ("b can bath now!");}}
Class bath, extends thread {public void run () {zys.out.println ("a is bathing!"); try {thread.sleep (20000);} catch (InterruptedException E ) {E.PrintStackTrace ();} // delayed 20 seconds effect system.out.println ("a Has Bathed!");}}; here is connected to the synchronization, but b may take more time, because It may just go to the door A. There is still not washed, so he goes to sleep again, and the result is finished just sleeping.
The second case:
B became more smart, so I didn't lose, if I knock a door 10 minutes, I may have to wait 10 minutes, but if I tap every second, I can't sleep, so I think a trick, Installed a agency, when A came out, the organ will pike the doorbell, so B can be high enough.
Class syn {public static void main (string [] args) throws exception {thread a = new bath (); a.Start (); // b int Time = 0; a.join (); system.out.println "B can bath now!");}} Class bathing extends thread {public void Run () {bathing ();} private void bath () {system.out.println ("a is bathing!"); Try {three .sleep (20000);} catch (interruptedException e) {E.PrintStackTrace ();} // delay 20 seconds Effect System.out.println ("a Has Bathed!");}};
As long as a is washed, B will be awakened, here A does not go to Notify him, but it is indirect notice B, of course, this can also be implemented in Wati and Notify, but it is not good.
Class syn {public static void main (string [] args) throws exception {thread a = new bath (); a.Start (); // b Int time = 0; synchronized (a) {a.wait ();} System.out.println ("b can bath now!");}}
Class bathing extends thread {public void Run () {synchronized (this) {bathing (); notify ();}} private void bathing ()}} private void bathing ()}} private void bathing ()}. Sleep (20000);} catch (interruptedException e) {E.PrintStackTrace ();} // delay 20 seconds Effect System.out.println ("a Has Bathed!");}};
For general objects, we must use Wait and Notify, but for a thread, the Join method sometimes more convenient.