English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

الفرق بين Wait وSleep في بايثون

Wait()-the thread releases ownership of this monitor and waits until another thread notifies the monitor of the object by callingnotify()method ornotifyAll()method wakes up the thread. Then, the thread waits until it can regain ownership of the monitor and resume execution.

Sleep()-this method makes the currently executing thread enter a sleep state (temporarily stop execution) for the specified number of milliseconds. The thread does not lose any ownership of monitors. It will send the current thread to the 'unrunnable' state for the specified time.

serial numberkeywaitingsleeping
1
class 
Wait() method belongs to Object class 
Sleep() method belongs to Thread class 
2
lock release 
Wait() releases the lock on the object 
it will not release the lock on the object 
3
call context
can call Wait() on the object itself 
can call Sleep() on the thread 
4。
wake-up condition
until the callnotify(),notifyAll()from the object
until at least the time expires or the call is interrupted
5
false wake-up 
the program may be falsely woken up 
it will not produce a false wake-up.

مثال على SynchronizedMap

synchronized(lockedObject){
   while(condition == true){
      lockedObject.wait() //releases lockedObject lock
   {}
   Thread.sleep(100); //puts current thread on Sleep
{}
أنت قد تحب