Question 1:
Why Wait () and notify () are methods in the object class, not methods in the thread class?
A: Because the lock is an arbitrary object, in the object class, if the lock object is not arbitrary in the thread class.
Question 2:
Why Wait () and notify () must be used in synchronous code fast or in a synchronous function?
A: Because Wait () and notify () are used when there is a lock, there is a lock in the synchronization code block.
Question 3:
Why Wait () and notify () must be called by the lock object?
A: Wait () and notify () to set up the thread pool with the lock object as an identifier.
Add:
Wait (): If a thread executes the wait () method, the thread will go into a thread pool waiting for the lock object as an identifier .
Notify (): If a thread executes the Notify () method, the thread wakes up a waiting thread in the thread pool with the lock object as an identifier.
Notifyall (): Wakes all waiting threads in the thread pool.
Attention:
1. The lock object is the same only in the same thread pool, otherwise in a different pool.
2.wait () and notify () must be called with the lock object, or the error will be immediately.
Java Multi-Threading 6 (thread and communication)