The difference between sleep and wait kinds of Java

1, sleep method of the Thread class is a static method, wait () Object is a member of the superclass

 

2, sleep () method results in a program to suspend the specified time, so that the cpu of the other threads, but his remains were monitoring state, when the specified time is up will automatically resume operation. During the call to sleep () method, the thread will not release the object lock.

And when the call wait () method, the thread will give up the object lock, waiting to enter the pool waiting for a lock for this object, the object is only for this call notify () method of the Thread object lock before entering the pool ready

 

3, sleep method needs to throw an exception, wait method does not require

 

 

4, sleep method can be used anywhere,

wait method can only be used in the synchronization method and a synchronization code block

Guess you like

Origin www.cnblogs.com/duguangming/p/11407778.html