First look at two concepts:
(1) lock pool: Suppose thread A has a lock an object (not class), while the other thread B, C wants to call a synchronized method of this object (or block), Since B, C thread must obtain ownership of the object lock object before entering the synchronized method (or block), the lock of the object happens is currently being occupied by the thread a, then B, C thread is blocked, enter a place to wait to release the lock, this place is the object lock pool.
(2) wait for the pool: Suppose A thread calls the wait method of an object, thread A releases the lock of the object while thread A enters into a waiting pool of the object, into the thread pool will not wait to compete lock of the object.
Again verify the role notify and notifyall.
See the following code:
At this point plus or notify notifyall methods, to wake up.
Now summarize the difference between action and notify and notifyall of:
- notifyAll will make all the threads in a wait pool all entered the lock pool to compete for the opportunity to acquire the lock;
- notify only randomly selected in a pool of threads waiting to enter the lock pool to compete for the opportunity to acquire a lock.