The difference between Java multi-threading and concurrency of -8.7 notify and notifyall

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/tanwenfang/article/details/92398075

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:
Here Insert Picture Description
At this point plus or notify notifyall methods, to wake up.
Here Insert Picture Description
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.

Guess you like

Origin blog.csdn.net/tanwenfang/article/details/92398075