Multithreaded learning - basics (7) The difference between sleep() and wait()

1. The difference between sleep() and wait() has in
common :
1. They are both in a multi-threaded environment, and they can block the specified milliseconds in the call of the program, and then continue to execute later (get it again in the current thread). after the execution right of the cpu).
2. Both wait() and sleep() can interrupt the suspended state of the thread through the interrupt() method, so that the thread immediately throws InterruptException.
If thread A wants to end thread B immediately, it can call the interrupt() method on the Thread instance of the thread B object. If thread B is in wait/sleep/join at the moment, thread B will immediately throw InterruptException, in catch(){ } return can safely end the thread.
It should be noted that
InterruptException is thrown internally by the thread itself, not by the Interruot() method itself. When interrupt() is called on a thread, if the thread is executing normal code, InterruptException will not be thrown, but once the thread enters wait()\join()\sleep(), InterruptException will be thrown immediately.

Differences:
1. Thread class methods: sleep(), yield(), etc.
Object methods: wait() and notify(), etc.
2. Each object has a lock to control synchronous access. The Synchronized keyword can interact with the object's lock to achieve thread synchronization.
The sleep method does not release the lock, while the wait method releases the lock so that other threads can use the synchronized control block or method.
3. wait, notify and notifyAll can only be used in synchronous control methods or synchronous control blocks, while sleep can be used anywhere
4. sleep and wait must catch exceptions, while notify and notifyAll do not need to catch exceptions
so sleep() and wait The biggest difference between () method is:
    when sleep() sleeps, it keeps the object lock and still holds the lock;
    while wait() sleeps, it releases the object lock.
  However, both wait() and sleep() can interrupt the suspended state of the thread through the interrupt() method, so that the thread immediately throws InterruptedException (but this method is not recommended).
The sleep() method
sleep() makes the current thread enter a stagnant state (blocks the current thread), and gives up the use of the CPU. The purpose is to prevent the current thread from occupying the CPU resources obtained by the process alone, so as to leave a certain time for other threads to execute. Opportunity;
   sleep() is the Static method of the Thread class; therefore, it cannot change the machine lock of the object, so when the Sleep() method is called in a Synchronized block, although the thread sleeps, the machine lock of the object does not change. The wood has not been released, and other threads cannot access the object (the object lock is held even if it is asleep).
  After the sleep() sleep time expires, the thread may not execute immediately because other threads may be running and not scheduled to give up unless the thread has a higher priority.
wait() method
The wait() method is a method in the Object class; when a thread executes the wait() method, it enters a waiting pool related to the object, and at the same time loses (releases) the object's machine lock (The machine lock is temporarily lost, and the object lock needs to be returned after the wait (long timeout) timeout period expires); other threads can access;
  wait() uses notify or notifyAll or the specified sleep time to wake up the thread in the current waiting pool.
  wiat() must be placed in a synchronized block, otherwise a "java.lang.IllegalMonitorStateException" will be thrown at program runtime.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324695077&siteId=291194637