- // waiter
- synchronized (object) {
- while (condition is not satisfied) {
- Object .wait ()
- }
- dosomething();
- }
- //informer
- synchronized (object) {
- Changing conditions
- Object .notifyAll ();
- }
When it comes to join now, we all know that the trial scene join method, that is, when we call in the thread B.join A thread (), only the thread A will enter BLOCK (actually WAITING OR TIMED_WAITING) state , when the execution is complete thread B after, a thread will continue to
look at the join to realize source, you will find inside also said wait - notification model
- public final synchronized void join(long millis)
- throws InterruptedException {
- long base = System.currentTimeMillis();
- long now = 0;
- if (millis < 0) {
- throw new IllegalArgumentException("timeout value is negative");
- }
- if (millis == 0) {
- while (isAlive()) {
- wait(0);
- }
- } else {
- while (isAlive()) {
- long delay = millis - now;
- if (delay <= 0) {
- break;
- }
- wait(delay);
- now = System.currentTimeMillis() - base;
- }
- }
- }
A thread that is first through synchronized, to acquire a lock thread B, then thread B while to determine whether survival, the survival of the wait blocks until the end of the exit B thread execution, calls notifyAll () method when the thread exits.
The reason to use this method while, in order to wake up after being re-confirm whether the conditions are satisfied.
So A thread will wait until the end of the thread execution will continue B