Thread state & the difference between Wait and sleep?
Thread stack content parsing:
The difference between Wait and sleep?
What they have in common: they all block, wait or sleep the current thread.
Differences:
Wait (): when a thread executes on the wait () method, the current thread releases the monitor lock, and other threads can occupy the lock. Once the execution of the wait () method is completed, the current thread continues to hold the lock until the scope of the lock is executed, as shown below:
Sleep (): sleep () has nothing to do with the lock operation, and if the method happens to be within the protection of a lock, the current thread maintains the monitor lock even when the sleep is executed. As shown below:
When a thread holds a lock, the thread stack prints a-locked
When a thread is waiting for another thread to release the lock, the thread stack prints a-waiting to lock
When a thread holds a lock but executes on the lock's wait, the thread stack first prints the locked and then prints-waiting on
The state of the thread
The RUNNABLE thread is running. (threads in Runnable state do not necessarily consume CPU)
TIMED_WAITING (on object monitor) indicates that the current thread has been suspended for a period of time, indicating that the thread is executing the obj.wait (ing time) method, and that the thread does not consume cpu.
TIMED_WAITING (sleeping) indicates that the current thread is suspended for a period of time and is executing the Thread.sleep (int time) method, such as:
WAITING (on object monitor) the current thread is suspended and is executing the parameterless obj.wait () method, which can only be woken up by notify, so it does not consume cpu