What is the difference between sleep() and wait()?

What is the difference between sleep() and wait()?

Both sleep() and wait() are methods of suspending execution of threads.

1. These two methods come from different classes, namely Thread and Object. The sleep method is a static method in the Thread class, and wait is a member method of the Object.
2. sleep() is a method of the thread class (Thread), which does not involve thread communication. When calling, it will suspend the thread for the specified time, but the monitoring will still be maintained, the object lock will not be released , and the time will automatically resume ; wait() is Object The method is used for communication between threads. When calling, it will give up the object lock and enter the waiting queue. After calling notify()/notifyAll() to wake up the specified thread or all threads, it will enter the object lock pool and prepare to obtain the object lock and enter the running state .
3. Wait, notify and notifyAll can only be used in synchronous control methods or synchronous control blocks , while sleep can be used anywhere (usage range).
4. The sleep() method must catch the exception InterruptedException, while wait()\notify() and notifyAll() do not need to catch the exception.

 

note:

  The sleep method only gives up the CPU, and does not release the synchronization resource lock.

  After the thread executes the sleep() method, it will enter the blocking state.

  The time specified by the sleep() method is the shortest time that the thread will not run. Therefore, the sleep() method cannot guarantee that the thread will start executing after the sleep expires.

  The role of notify is equivalent to waking up the sleeping person without assigning tasks to him, that is, notify only allows the thread that previously called wait to have the right to participate in thread scheduling again.

explain:

  sleep: The method defined in the Thread class, which means that the thread sleeps and will automatically wake up;
  wait: the method defined in the Object, you need to manually call the notify() or notifyAll() method.
  Sleep is a method of the thread class (Thread), which causes this thread to suspend execution for a specified time, giving the execution opportunity to other threads, but the monitoring state is still maintained and will automatically resume after that time. Calling sleep will not release the object lock. Wait is a method of the Object class. Calling the wait method on this object causes the thread to give up the object lock and enter the waiting lock pool waiting for this object. Only after the notify method (or notifyAll) is issued for this object, the thread enters the object lock pool and is ready to obtain The object lock enters the running state.
  Sleep is the thread that is executing actively give up cpu, cpu to execute other threads, after the time specified by sleep, cpu will return to this thread to continue execution, if the current thread enters the synchronization lock, the sleep method will not Release the lock. Even if the current thread uses the sleep method to give up the cpu, other threads blocked by the synchronization lock cannot be executed. Wait refers to a thread that has entered the synchronization lock, allowing itself to temporarily give up the synchronization lock, so that other threads waiting for this lock can get the synchronization lock and run, only other threads call the notify method (notify does not release the lock , Just tells the thread that has called the wait method to participate in the competition to obtain the lock, but it is not immediately obtained, because the lock is still in the hands of others and others have not released it. If there is a lot of code behind the notify method, these codes are needed The lock will be released after execution, you can add a wait and some code after the notfiy method to see the effect), the thread that calls the wait method will release the wait state and the program can continue to run down after it gets the lock again.

Guess you like

Origin blog.csdn.net/yangguoqi/article/details/106243066