Wait and sleep differences
common denominator:
1. They are all in multi-threaded environment, can block the number of milliseconds specified at the call of the program, and return.
2. Wait () and sleep () can break the thread's suspend state through the interrupt () method, causing the thread to throw interruptedexception immediately.
If thread A wants to end thread B immediately, you can call the interrupt method on the thread instance that corresponds to threads B. If thread B is wait/sleep/join at the moment, thread B will immediately throw interruptedexception, and return directly in catch () {} to safely end the thread.
It is important to note that Interruptedexception is thrown from within the thread itself, not by the interrupt () method. When you call interrupt () on a thread, the thread does not throw interruptedexception at all if it is executing normal code. However, once the thread has entered wait ()/sleep ()/join (), the interruptedexception is immediately thrown.
different points:
1. Method of the Thread class: sleep (), yield (), etc.
method of object: 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, and the wait method frees the lock so that other threads can use the synchronization control block or method.
3. Wait,notify and Notifyall can only be used in a synchronous control method or in a sync control block, and sleep can be used anywhere
4. Sleep must catch exceptions, while wait,notify and notifyall do not need to catch exceptions
so the biggest difference between the sleep () and wait () methods is:
sleep () when sleeping, keep the object lock, still occupy the lock;
while Wait () sleeps, the object lock is released.
but wait () and sleep () can break the thread's suspend state through the interrupt () method, causing the thread to throw interruptedexception immediately (but not recommended).
sleep () method
sleep () causes the current thread to go to a standstill (blocking the current thread), giving up the use of the cup in order not to allow the current thread to occupy the CPU resources obtained by the process on its own, to leave a certain amount of time for other threads to execute the opportunity;
sleep () is the static (static) method of the thread class, so he cannot change the machine lock of the object, so when the sleep () method is called in a synchronized block, the thread sleeps, but the object's machine lock and the wood is released. Other threads cannot access this object (even if asleep also holds the object lock).
after the sleep () sleep time expires, the thread does not necessarily execute immediately, because other threads may be running and are not scheduled to abort execution unless the thread has a higher priority.
Wait () method
The Wait () method is a method in the object class, and when a thread executes to the wait () method, it enters into a waiting pool associated with the object and loses (frees) the object's machine lock (temporarily loses the machine lock, wait (long timeout) Return the object lock after the timeout period, other threads can access;
Wait () wakes the threads in the current waiting pool using notify or notifyalll or specifying sleep time.
Wiat () must be placed in the synchronized block, otherwise the "java.lang.IllegalMonitorStateException" exception will be thrown when the program runtime.
Multithreading wait and sleep differences