join()方法
1、现在有T1、T2、T3三个线程,你怎样保证T2在T1执行完后执行,T3在T2执行完后执行?
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.mianshi.easy; 2 class JoinDemo implements Runnable{ 3 public void run() { 4 for(int i=0;i<100;i++){ 5 System.out.println(Thread.currentThread().getName()+"……"+ i); 6 } 7 } 8 } 9 10 public class JoinMethod { 11 12 public static void main(String[] args) throws InterruptedException { 13 JoinDemo jd = new JoinDemo(); 14 Thread T1 = new Thread(jd); 15 Thread T2 = new Thread(jd); 16 Thread T3 = new Thread(jd); 17 18 T1.start(); 19 //当t1拿着执行权把这些数据都打印完了,主线程才恢复到运行中来 20 T1.join(); 21 22 T2.start(); 23 T2.join(); 24 25 T3.start(); 26 T3.join(); 27 28 } 29 }
结果:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
Thread-0……0 Thread-0……1 Thread-0……2 Thread-0……3 Thread-0……4 Thread-0……5 Thread-0……6 Thread-0……7 Thread-0……8 Thread-0……9 Thread-0……10 Thread-0……11 Thread-0……12 Thread-0……13 Thread-0……14 Thread-0……15 Thread-0……16 Thread-0……17 Thread-0……18 Thread-0……19 Thread-0……20 Thread-0……21 Thread-0……22 Thread-0……23 Thread-0……24 Thread-0……25 Thread-0……26 Thread-0……27 Thread-0……28 Thread-0……29 Thread-0……30 Thread-0……31 Thread-0……32 Thread-0……33 Thread-0……34 Thread-0……35 Thread-0……36 Thread-0……37 Thread-0……38 Thread-0……39 Thread-0……40 Thread-0……41 Thread-0……42 Thread-0……43 Thread-0……44 Thread-0……45 Thread-0……46 Thread-0……47 Thread-0……48 Thread-0……49 Thread-0……50 Thread-0……51 Thread-0……52 Thread-0……53 Thread-0……54 Thread-0……55 Thread-0……56 Thread-0……57 Thread-0……58 Thread-0……59 Thread-0……60 Thread-0……61 Thread-0……62 Thread-0……63 Thread-0……64 Thread-0……65 Thread-0……66 Thread-0……67 Thread-0……68 Thread-0……69 Thread-0……70 Thread-0……71 Thread-0……72 Thread-0……73 Thread-0……74 Thread-0……75 Thread-0……76 Thread-0……77 Thread-0……78 Thread-0……79 Thread-0……80 Thread-0……81 Thread-0……82 Thread-0……83 Thread-0……84 Thread-0……85 Thread-0……86 Thread-0……87 Thread-0……88 Thread-0……89 Thread-0……90 Thread-0……91 Thread-0……92 Thread-0……93 Thread-0……94 Thread-0……95 Thread-0……96 Thread-0……97 Thread-0……98 Thread-0……99 Thread-1……0 Thread-1……1 Thread-1……2 Thread-1……3 Thread-1……4 Thread-1……5 Thread-1……6 Thread-1……7 Thread-1……8 Thread-1……9 Thread-1……10 Thread-1……11 Thread-1……12 Thread-1……13 Thread-1……14 Thread-1……15 Thread-1……16 Thread-1……17 Thread-1……18 Thread-1……19 Thread-1……20 Thread-1……21 Thread-1……22 Thread-1……23 Thread-1……24 Thread-1……25 Thread-1……26 Thread-1……27 Thread-1……28 Thread-1……29 Thread-1……30 Thread-1……31 Thread-1……32 Thread-1……33 Thread-1……34 Thread-1……35 Thread-1……36 Thread-1……37 Thread-1……38 Thread-1……39 Thread-1……40 Thread-1……41 Thread-1……42 Thread-1……43 Thread-1……44 Thread-1……45 Thread-1……46 Thread-1……47 Thread-1……48 Thread-1……49 Thread-1……50 Thread-1……51 Thread-1……52 Thread-1……53 Thread-1……54 Thread-1……55 Thread-1……56 Thread-1……57 Thread-1……58 Thread-1……59 Thread-1……60 Thread-1……61 Thread-1……62 Thread-1……63 Thread-1……64 Thread-1……65 Thread-1……66 Thread-1……67 Thread-1……68 Thread-1……69 Thread-1……70 Thread-1……71 Thread-1……72 Thread-1……73 Thread-1……74 Thread-1……75 Thread-1……76 Thread-1……77 Thread-1……78 Thread-1……79 Thread-1……80 Thread-1……81 Thread-1……82 Thread-1……83 Thread-1……84 Thread-1……85 Thread-1……86 Thread-1……87 Thread-1……88 Thread-1……89 Thread-1……90 Thread-1……91 Thread-1……92 Thread-1……93 Thread-1……94 Thread-1……95 Thread-1……96 Thread-1……97 Thread-1……98 Thread-1……99 Thread-2……0 Thread-2……1 Thread-2……2 Thread-2……3 Thread-2……4 Thread-2……5 Thread-2……6 Thread-2……7 Thread-2……8 Thread-2……9 Thread-2……10 Thread-2……11 Thread-2……12 Thread-2……13 Thread-2……14 Thread-2……15 Thread-2……16 Thread-2……17 Thread-2……18 Thread-2……19 Thread-2……20 Thread-2……21 Thread-2……22 Thread-2……23 Thread-2……24 Thread-2……25 Thread-2……26 Thread-2……27 Thread-2……28 Thread-2……29 Thread-2……30 Thread-2……31 Thread-2……32 Thread-2……33 Thread-2……34 Thread-2……35 Thread-2……36 Thread-2……37 Thread-2……38 Thread-2……39 Thread-2……40 Thread-2……41 Thread-2……42 Thread-2……43 Thread-2……44 Thread-2……45 Thread-2……46 Thread-2……47 Thread-2……48 Thread-2……49 Thread-2……50 Thread-2……51 Thread-2……52 Thread-2……53 Thread-2……54 Thread-2……55 Thread-2……56 Thread-2……57 Thread-2……58 Thread-2……59 Thread-2……60 Thread-2……61 Thread-2……62 Thread-2……63 Thread-2……64 Thread-2……65 Thread-2……66 Thread-2……67 Thread-2……68 Thread-2……69 Thread-2……70 Thread-2……71 Thread-2……72 Thread-2……73 Thread-2……74 Thread-2……75 Thread-2……76 Thread-2……77 Thread-2……78 Thread-2……79 Thread-2……80 Thread-2……81 Thread-2……82 Thread-2……83 Thread-2……84 Thread-2……85 Thread-2……86 Thread-2……87 Thread-2……88 Thread-2……89 Thread-2……90 Thread-2……91 Thread-2……92 Thread-2……93 Thread-2……94 Thread-2……95 Thread-2……96 Thread-2……97 Thread-2……98 Thread-2……99
2、练习demo
(1)主线程向下转时,碰到了t1.join(),t1要申请加入到运行中来,就是要CPU执行权。这时CPU执行权在主线程手里,主线程就把CPU执行权给放开,陷入冻结状态。活着的只有t1了,只有当t1拿着执行权把这些数据都打印完了,主线程才恢复到运行中来。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.mianshi.easy; 2 3 class JoinDemo implements Runnable{ 4 5 public void run() { 6 for(int i=0;i<100;i++){ 7 System.out.println(Thread.currentThread().getName()+"……"+ i); 8 } 9 } 10 } 11 12 /** 13 * 主线程向下转时,碰到了t1.join(),t1要申请加入到运行中来,就是要CPU执行权。 14 * 这时候CPU执行权在主线程手里,主线程就把CPU执行权给放开,陷入冻结状态。活着的 15 * 只有t1了,只有当t1拿着执行权把这些数据都打印完了,主线程才恢复到运行中来。 16 * */ 17 public class JoinMethod { 18 19 public static void main(String[] args) throws InterruptedException { 20 JoinDemo jd = new JoinDemo(); 21 Thread t1 = new Thread(jd); 22 Thread t2 = new Thread(jd); 23 24 t1.start(); 25 26 t1.join(); 27 28 t2.start(); 29 30 for(int j =0; j<100; j++){ 31 System.out.println(Thread.currentThread().getName()+"……"+j); 32 } 33 } 34 }
结果:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
Thread-0……0 Thread-0……1 Thread-0……2 Thread-0……3 Thread-0……4 Thread-0……5 Thread-0……6 Thread-0……7 Thread-0……8 Thread-0……9 Thread-0……10 Thread-0……11 Thread-0……12 Thread-0……13 Thread-0……14 Thread-0……15 Thread-0……16 Thread-0……17 Thread-0……18 Thread-0……19 Thread-0……20 Thread-0……21 Thread-0……22 Thread-0……23 Thread-0……24 Thread-0……25 Thread-0……26 Thread-0……27 Thread-0……28 Thread-0……29 Thread-0……30 Thread-0……31 Thread-0……32 Thread-0……33 Thread-0……34 Thread-0……35 Thread-0……36 Thread-0……37 Thread-0……38 Thread-0……39 Thread-0……40 Thread-0……41 Thread-0……42 Thread-0……43 Thread-0……44 Thread-0……45 Thread-0……46 Thread-0……47 Thread-0……48 Thread-0……49 Thread-0……50 Thread-0……51 Thread-0……52 Thread-0……53 Thread-0……54 Thread-0……55 Thread-0……56 Thread-0……57 Thread-0……58 Thread-0……59 Thread-0……60 Thread-0……61 Thread-0……62 Thread-0……63 Thread-0……64 Thread-0……65 Thread-0……66 Thread-0……67 Thread-0……68 Thread-0……69 Thread-0……70 Thread-0……71 Thread-0……72 Thread-0……73 Thread-0……74 Thread-0……75 Thread-0……76 Thread-0……77 Thread-0……78 Thread-0……79 Thread-0……80 Thread-0……81 Thread-0……82 Thread-0……83 Thread-0……84 Thread-0……85 Thread-0……86 Thread-0……87 Thread-0……88 Thread-0……89 Thread-0……90 Thread-0……91 Thread-0……92 Thread-0……93 Thread-0……94 Thread-0……95 Thread-0……96 Thread-0……97 Thread-0……98 Thread-0……99 main……0 main……1 main……2 main……3 main……4 main……5 main……6 main……7 main……8 main……9 main……10 main……11 main……12 main……13 main……14 main……15 main……16 main……17 main……18 main……19 main……20 main……21 main……22 main……23 main……24 main……25 main……26 main……27 main……28 main……29 main……30 main……31 main……32 main……33 main……34 main……35 Thread-1……0 main……36 Thread-1……1 main……37 Thread-1……2 main……38 Thread-1……3 main……39 Thread-1……4 main……40 Thread-1……5 main……41 Thread-1……6 Thread-1……7 main……42 Thread-1……8 main……43 Thread-1……9 main……44 Thread-1……10 main……45 Thread-1……11 Thread-1……12 main……46 Thread-1……13 main……47 Thread-1……14 main……48 Thread-1……15 main……49 Thread-1……16 main……50 Thread-1……17 main……51 Thread-1……18 main……52 Thread-1……19 main……53 Thread-1……20 Thread-1……21 Thread-1……22 Thread-1……23 Thread-1……24 Thread-1……25 main……54 main……55 main……56 main……57 Thread-1……26 main……58 Thread-1……27 main……59 Thread-1……28 main……60 Thread-1……29 main……61 Thread-1……30 main……62 Thread-1……31 main……63 Thread-1……32 main……64 Thread-1……33 main……65 Thread-1……34 main……66 Thread-1……35 main……67 Thread-1……36 main……68 Thread-1……37 main……69 Thread-1……38 main……70 Thread-1……39 main……71 Thread-1……40 main……72 Thread-1……41 main……73 Thread-1……42 main……74 Thread-1……43 main……75 Thread-1……44 main……76 Thread-1……45 main……77 Thread-1……46 main……78 Thread-1……47 main……79 Thread-1……48 main……80 Thread-1……49 main……81 Thread-1……50 main……82 Thread-1……51 main……83 Thread-1……52 main……84 Thread-1……53 main……85 Thread-1……54 main……86 Thread-1……55 main……87 Thread-1……56 main……88 Thread-1……57 main……89 Thread-1……58 main……90 Thread-1……59 main……91 Thread-1……60 main……92 Thread-1……61 main……93 Thread-1……62 main……94 Thread-1……63 main……95 Thread-1……64 main……96 Thread-1……65 main……97 Thread-1……66 main……98 Thread-1……67 main……99 Thread-1……68 Thread-1……69 Thread-1……70 Thread-1……71 Thread-1……72 Thread-1……73 Thread-1……74 Thread-1……75 Thread-1……76 Thread-1……77 Thread-1……78 Thread-1……79 Thread-1……80 Thread-1……81 Thread-1……82 Thread-1……83 Thread-1……84 Thread-1……85 Thread-1……86 Thread-1……87 Thread-1……88 Thread-1……89 Thread-1……90 Thread-1……91 Thread-1……92 Thread-1……93 Thread-1……94 Thread-1……95 Thread-1……96 Thread-1……97 Thread-1……98 Thread-1……99
(2)改变顺序,主线程启动了两个子线程A、B时,其中一个子线程A调用了join()方法后,主线程将释放执行权,进入冻结状态。子线程A并不会影响子线程B,两个子线程将会交替执行,直到A线程run()方法执行完,主线程将会重新获得执行权。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.mianshi.easy; 2 3 class JoinDemo implements Runnable{ 4 5 public void run() { 6 for(int i=0;i<200;i++){ 7 System.out.println(Thread.currentThread().getName()+"……"+ i); 8 } 9 } 10 } 11 12 /** 13 * 将t1.join()和t2.start()位置互换。主线程开启了t1、t2,这时候CPU执行权还在主线程手里。 14 * 当主线程碰到了t1.join(),释放执行权,处于冻结状态。活着的t1、t2都具备执行资格,这时候CPU 15 * 便对t1和t2交替执行。主线程要等到t1结束才能活,至于t2结不结束,与主线程没有丝毫关系。如果t1 16 * 结束了,t2还没结束,主线程就会和t2抢执行权执行。主线程碰到谁的join,它就等谁。也就是说,谁 17 * 让它把执行权放出来,它就等谁死。至于谁去抢,它不管。 18 * 19 * */ 20 public class JoinMethod { 21 22 public static void main(String[] args) throws InterruptedException { 23 JoinDemo jd = new JoinDemo(); 24 Thread t1 = new Thread(jd); 25 Thread t2 = new Thread(jd); 26 27 t1.start(); 28 29 t2.start(); 30 31 t1.join(); 32 33 for(int j =0; j<100; j++){ 34 System.out.println(Thread.currentThread().getName()+"……"+j); 35 } 36 } 37 }
结果:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
Thread-1……0 Thread-0……0 Thread-0……1 Thread-1……1 Thread-0……2 Thread-1……2 Thread-0……3 Thread-1……3 Thread-0……4 Thread-0……5 Thread-0……6 Thread-0……7 Thread-0……8 Thread-0……9 Thread-1……4 Thread-1……5 Thread-1……6 Thread-1……7 Thread-1……8 Thread-1……9 Thread-1……10 Thread-1……11 Thread-1……12 Thread-0……10 Thread-1……13 Thread-1……14 Thread-1……15 Thread-1……16 Thread-1……17 Thread-1……18 Thread-1……19 Thread-1……20 Thread-1……21 Thread-1……22 Thread-1……23 Thread-1……24 Thread-1……25 Thread-1……26 Thread-0……11 Thread-0……12 Thread-0……13 Thread-0……14 Thread-0……15 Thread-0……16 Thread-0……17 Thread-1……27 Thread-0……18 Thread-1……28 Thread-0……19 Thread-1……29 Thread-0……20 Thread-1……30 Thread-0……21 Thread-1……31 Thread-1……32 Thread-1……33 Thread-1……34 Thread-1……35 Thread-1……36 Thread-1……37 Thread-1……38 Thread-1……39 Thread-1……40 Thread-1……41 Thread-1……42 Thread-1……43 Thread-1……44 Thread-1……45 Thread-1……46 Thread-1……47 Thread-0……22 Thread-1……48 Thread-0……23 Thread-1……49 Thread-0……24 Thread-1……50 Thread-0……25 Thread-1……51 Thread-0……26 Thread-1……52 Thread-0……27 Thread-1……53 Thread-0……28 Thread-1……54 Thread-0……29 Thread-1……55 Thread-0……30 Thread-1……56 Thread-0……31 Thread-1……57 Thread-1……58 Thread-1……59 Thread-1……60 Thread-1……61 Thread-1……62 Thread-1……63 Thread-1……64 Thread-1……65 Thread-1……66 Thread-1……67 Thread-1……68 Thread-1……69 Thread-1……70 Thread-1……71 Thread-0……32 Thread-1……72 Thread-0……33 Thread-1……73 Thread-0……34 Thread-1……74 Thread-0……35 Thread-1……75 Thread-0……36 Thread-1……76 Thread-0……37 Thread-1……77 Thread-0……38 Thread-0……39 Thread-0……40 Thread-0……41 Thread-0……42 Thread-0……43 Thread-1……78 Thread-0……44 Thread-1……79 Thread-0……45 Thread-1……80 Thread-0……46 Thread-1……81 Thread-0……47 Thread-1……82 Thread-0……48 Thread-1……83 Thread-0……49 Thread-1……84 Thread-0……50 Thread-1……85 Thread-0……51 Thread-1……86 Thread-0……52 Thread-1……87 Thread-0……53 Thread-1……88 Thread-0……54 Thread-1……89 Thread-0……55 Thread-1……90 Thread-0……56 Thread-1……91 Thread-0……57 Thread-1……92 Thread-0……58 Thread-1……93 Thread-0……59 Thread-1……94 Thread-0……60 Thread-1……95 Thread-0……61 Thread-1……96 Thread-0……62 Thread-1……97 Thread-0……63 Thread-1……98 Thread-0……64 Thread-1……99 Thread-0……65 Thread-1……100 Thread-0……66 Thread-1……101 Thread-0……67 Thread-1……102 Thread-0……68 Thread-1……103 Thread-0……69 Thread-1……104 Thread-0……70 Thread-1……105 Thread-0……71 Thread-1……106 Thread-0……72 Thread-1……107 Thread-0……73 Thread-1……108 Thread-1……109 Thread-0……74 Thread-1……110 Thread-0……75 Thread-1……111 Thread-0……76 Thread-1……112 Thread-0……77 Thread-1……113 Thread-1……114 Thread-0……78 Thread-1……115 Thread-0……79 Thread-1……116 Thread-0……80 Thread-1……117 Thread-0……81 Thread-1……118 Thread-0……82 Thread-1……119 Thread-0……83 Thread-1……120 Thread-0……84 Thread-1……121 Thread-0……85 Thread-1……122 Thread-0……86 Thread-1……123 Thread-0……87 Thread-1……124 Thread-0……88 Thread-1……125 Thread-0……89 Thread-1……126 Thread-0……90 Thread-1……127 Thread-0……91 Thread-1……128 Thread-0……92 Thread-1……129 Thread-0……93 Thread-1……130 Thread-0……94 Thread-1……131 Thread-0……95 Thread-1……132 Thread-0……96 Thread-1……133 Thread-0……97 Thread-1……134 Thread-0……98 Thread-1……135 Thread-0……99 Thread-1……136 Thread-0……100 Thread-1……137 Thread-0……101 Thread-1……138 Thread-0……102 Thread-1……139 Thread-0……103 Thread-1……140 Thread-0……104 Thread-1……141 Thread-0……105 Thread-1……142 Thread-0……106 Thread-1……143 Thread-0……107 Thread-1……144 Thread-0……108 Thread-1……145 Thread-1……146 Thread-1……147 Thread-0……109 Thread-1……148 Thread-0……110 Thread-1……149 Thread-0……111 Thread-1……150 Thread-0……112 Thread-1……151 Thread-0……113 Thread-1……152 Thread-0……114 Thread-1……153 Thread-0……115 Thread-1……154 Thread-0……116 Thread-1……155 Thread-0……117 Thread-1……156 Thread-0……118 Thread-1……157 Thread-0……119 Thread-1……158 Thread-0……120 Thread-1……159 Thread-0……121 Thread-0……122 Thread-1……160 Thread-0……123 Thread-1……161 Thread-0……124 Thread-1……162 Thread-0……125 Thread-0……126 Thread-0……127 Thread-0……128 Thread-0……129 Thread-0……130 Thread-0……131 Thread-0……132 Thread-0……133 Thread-0……134 Thread-0……135 Thread-0……136 Thread-0……137 Thread-0……138 Thread-0……139 Thread-0……140 Thread-0……141 Thread-0……142 Thread-0……143 Thread-0……144 Thread-0……145 Thread-0……146 Thread-0……147 Thread-0……148 Thread-0……149 Thread-0……150 Thread-0……151 Thread-0……152 Thread-0……153 Thread-0……154 Thread-0……155 Thread-0……156 Thread-0……157 Thread-0……158 Thread-0……159 Thread-0……160 Thread-0……161 Thread-0……162 Thread-0……163 Thread-0……164 Thread-0……165 Thread-0……166 Thread-0……167 Thread-0……168 Thread-0……169 Thread-0……170 Thread-0……171 Thread-0……172 Thread-0……173 Thread-0……174 Thread-0……175 Thread-0……176 Thread-0……177 Thread-0……178 Thread-0……179 Thread-0……180 Thread-0……181 Thread-0……182 Thread-0……183 Thread-0……184 Thread-0……185 Thread-0……186 Thread-0……187 Thread-0……188 Thread-0……189 Thread-0……190 Thread-0……191 Thread-0……192 Thread-0……193 Thread-0……194 Thread-0……195 Thread-0……196 Thread-0……197 Thread-0……198 Thread-0……199 Thread-1……163 Thread-1……164 main……0 main……1 main……2 main……3 main……4 main……5 main……6 main……7 main……8 main……9 main……10 main……11 main……12 main……13 main……14 main……15 main……16 main……17 main……18 main……19 main……20 main……21 main……22 main……23 main……24 main……25 main……26 main……27 main……28 main……29 main……30 main……31 main……32 main……33 main……34 main……35 main……36 main……37 main……38 main……39 main……40 main……41 main……42 main……43 main……44 main……45 main……46 main……47 main……48 main……49 main……50 main……51 main……52 main……53 main……54 main……55 main……56 main……57 main……58 main……59 Thread-1……165 main……60 Thread-1……166 main……61 Thread-1……167 main……62 main……63 main……64 main……65 main……66 main……67 main……68 main……69 main……70 main……71 main……72 main……73 main……74 main……75 main……76 Thread-1……168 main……77 Thread-1……169 main……78 Thread-1……170 main……79 Thread-1……171 main……80 Thread-1……172 main……81 Thread-1……173 main……82 Thread-1……174 main……83 Thread-1……175 main……84 Thread-1……176 main……85 Thread-1……177 main……86 Thread-1……178 main……87 Thread-1……179 main……88 Thread-1……180 main……89 Thread-1……181 main……90 Thread-1……182 main……91 Thread-1……183 main……92 Thread-1……184 main……93 Thread-1……185 main……94 Thread-1……186 main……95 Thread-1……187 main……96 Thread-1……188 main……97 Thread-1……189 main……98 Thread-1……190 main……99 Thread-1……191 Thread-1……192 Thread-1……193 Thread-1……194 Thread-1……195 Thread-1……196 Thread-1……197 Thread-1……198 Thread-1……199
顺便贴下java.lang.Thread中关于join()方法的实现代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * Waits at most {@code millis} milliseconds for this thread to 3 * die. A timeout of {@code 0} means to wait forever. 4 * 5 * <p> This implementation uses a loop of {@code this.wait} calls 6 * conditioned on {@code this.isAlive}. As a thread terminates the 7 * {@code this.notifyAll} method is invoked. It is recommended that 8 * applications not use {@code wait}, {@code notify}, or 9 * {@code notifyAll} on {@code Thread} instances. 10 * 11 * @param millis 12 * the time to wait in milliseconds 13 * 14 * @throws IllegalArgumentException 15 * if the value of {@code millis} is negative 16 * 17 * @throws InterruptedException 18 * if any thread has interrupted the current thread. The 19 * <i>interrupted status</i> of the current thread is 20 * cleared when this exception is thrown. 21 */ 22 public final synchronized void join(long millis) 23 throws InterruptedException { 24 long base = System.currentTimeMillis(); 25 long now = 0; 26 27 if (millis < 0) { 28 throw new IllegalArgumentException("timeout value is negative"); 29 } 30 31 if (millis == 0) { 32 while (isAlive()) { 33 wait(0); 34 } 35 } else { 36 while (isAlive()) { 37 long delay = millis - now; 38 if (delay <= 0) { 39 break; 40 } 41 wait(delay); 42 now = System.currentTimeMillis() - base; 43 } 44 } 45 } 46 47 /** 48 * Waits at most {@code millis} milliseconds plus 49 * {@code nanos} nanoseconds for this thread to die. 50 * 51 * <p> This implementation uses a loop of {@code this.wait} calls 52 * conditioned on {@code this.isAlive}. As a thread terminates the 53 * {@code this.notifyAll} method is invoked. It is recommended that 54 * applications not use {@code wait}, {@code notify}, or 55 * {@code notifyAll} on {@code Thread} instances. 56 * 57 * @param millis 58 * the time to wait in milliseconds 59 * 60 * @param nanos 61 * {@code 0-999999} additional nanoseconds to wait 62 * 63 * @throws IllegalArgumentException 64 * if the value of {@code millis} is negative, or the value 65 * of {@code nanos} is not in the range {@code 0-999999} 66 * 67 * @throws InterruptedException 68 * if any thread has interrupted the current thread. The 69 * <i>interrupted status</i> of the current thread is 70 * cleared when this exception is thrown. 71 */ 72 public final synchronized void join(long millis, int nanos) 73 throws InterruptedException { 74 75 if (millis < 0) { 76 throw new IllegalArgumentException("timeout value is negative"); 77 } 78 79 if (nanos < 0 || nanos > 999999) { 80 throw new IllegalArgumentException( 81 "nanosecond timeout value out of range"); 82 } 83 84 if (nanos >= 500000 || (nanos != 0 && millis == 0)) { 85 millis++; 86 } 87 88 join(millis); 89 } 90 91 /** 92 * Waits for this thread to die. 93 * 94 * <p> An invocation of this method behaves in exactly the same 95 * way as the invocation 96 * 97 * <blockquote> 98 * {@linkplain #join(long) join}{@code (0)} 99 * </blockquote> 100 * 101 * @throws InterruptedException 102 * if any thread has interrupted the current thread. The 103 * <i>interrupted status</i> of the current thread is 104 * cleared when this exception is thrown. 105 */ 106 public final void join() throws InterruptedException { 107 join(0); 108 }