The essence thread.join

Java code    Collection Code
  1. // waiter  
  2. synchronized (object) {  
  3.     while (condition is not satisfied) {  
  4.         Object .wait ()  
  5.     }  
  6.     dosomething();  
  7. }  
  8. //informer  
  9. synchronized (object) {  
  10.     Changing conditions  
  11.     Object .notifyAll ();  
  12. }  


    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 

Java code    Collection Code
  1. public final synchronized void join(long millis)  
  2.     throws InterruptedException {  
  3.         long base = System.currentTimeMillis();  
  4.         long now = 0;  
  5.   
  6.         if (millis < 0) {  
  7.             throw new IllegalArgumentException("timeout value is negative");  
  8.         }  
  9.   
  10.         if (millis == 0) {  
  11.             while (isAlive()) {  
  12.                 wait(0);  
  13.             }  
  14.         } else {  
  15.             while (isAlive()) {  
  16.                 long delay = millis - now;  
  17.                 if (delay <= 0) {  
  18.                     break;  
  19.                 }  
  20.                 wait(delay);  
  21.                 now = System.currentTimeMillis() - base;  
  22.             }  
  23.         }  
  24.     }  


    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

Guess you like

Origin www.cnblogs.com/silyvin/p/11374368.html