3 , Wait () and notify (), Notifyall ()
Wait (): Causes the current thread to wait until another thread invokes the Notify () method or the Notifyall () method for T His object.
Notify (): Wakes up a single thread, that's waiting on this object ' s monitor.
Notifyall (): Wakes up all threads is waiting on this object ' s monitor.
These three methods are used to coordinate the access of multiple threads to shared data, so they must be used within the synchronized statement block .
The Synchronized keyword is used to protect shared data and prevent other threads from accessing the shared data, but the process of the program is inflexible and how can other threads have access to shared data when the current thread has not exited the synchronized data block? At this point, use these three methods to control flexibly. The wait () method suspends the current thread and releases the object lock, allowing other threads to enter the synchronized data block, where the current thread is placed in the object waiting pool. When the Notify () method is called, an arbitrary thread is removed from the object's wait pool and placed in the lock flag waiting pool, and only the lock flag waits for the thread to acquire the lock flag, and notify () does not work if there are no threads in the lock flag waiting pool.
Notifyall () removes all the threads waiting for that object from the object waiting pool and puts it in the lock flag waiting pool. Note that these three methods are java.lang.Object methods.
Java Wait () and notify (), Notifyall ()