the Difference between Sleep and Wait in Java Multithread

Sleep is used for thread control, and Wait is used for the communication between threads, and the methods to cooperate Wait are also notify and notifyAll.

Difference one:
sleep is the method of the Thread class, is used to control its own process by thread, such as a thread to chime print out a time in every second, then I need to add a Sleep in front of the Print method so as to run once every seconds. As an alarm clock.

wait is the method of the Object class, is used for communication between threads, this method will let the current process has Object Lock to wait to know when the other thread calls the notify method and then wake up, but you can give him a specified time, automatic wake up . This method is mainly used to schedule between the different threads.

Difference two:
The release of the Lock, assuming you had already know where the concept of a lock and its significance. Call Sleep method does not release the Lock (my feeling is that Sleep is no relation with Lock, because it is a method for threads managing their own, does not involve thread communication)

Explained in JDK 7:
"Public static void sleep (long millis)

throws InterruptedException
Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers.The thread does not lose ownership of any monitors.

public final void wait () throws InterruptedException
Causes the current thread to wait until another thread invokes the notify () method or the notifyAll () method for this object. In other words, this method behaves exactly as if it simply performs the call wait (0). The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution. "
Call wait method releases the lock the current thread (in fact, communication between threads is managed by the object, all operations of an object thread is the object through their own approach to the management of the wait, as if the object is a TV, three the three threads, then the TV's remote control is the lock, if A is now holding the remote control, TV calls the wait method, then A on the surrender of their own remote control, by the jVM virtual machine scheduling, the remote control to who.) [I think of a funny example: If you take remote control of A, he can sleep with their tune a station every ten minutes, and transfer station in his ten-minute rest period, the remote control still in his hand ~]

Difference three:
Use the region
As the Wait function is of special significance, so it should be placed in a synchronized block,that it is significant.

Note: Both methods require an exception.

Personal opinion: Thread synchronization management, whether was scheduled by the Object, if it is scheduled by an object , then the new ReentrantLock mechanism the JDK 1.5 introduced is more worthwhile than synchronized keyword to advocate. Because it was such a mechanism to better reflect. A lot of people can not understand the difference between Wait and Sleep, I think that is because the impact of the synchronized keyword.
This address is http://www.computerites.com/web-design-skills/2011/12/the-difference-between-sleep-and-wait-in-java-multithread-280.html



This address is http://www.computerites.com/web-design-skills/2011/12/the-difference-between-sleep-and-wait-in-java-multithread-280.html