标签:
public final void join() throws InterruptedException
InterruptedException
- 假设不论什么线程中断了当前线程。当抛出该异常时,当前线程的中断状态 被清除。public final void join(long millis) throws InterruptedException
millis
毫秒。超时为 0
意味着要一直等下去。
millis
- 以毫秒为单位的等待时间。 InterruptedException
- 假设不论什么线程中断了当前线程。当抛出该异常时,当前线程的中断状态 被清除。
public final void join(long millis, int nanos) throws InterruptedException
millis
毫秒 + nanos
纳秒。
millis
- 以毫秒为单位的等待时间。
nanos
- 要等待的 0-999999 附加纳秒。
IllegalArgumentException
- 假设 millis 值为负,则 nanos 的值不在 0-999999 范围内。InterruptedException
- 假设不论什么线程中断了当前线程。当抛出该异常时。当前线程的中断状态 被清除。
Thread.join()。是用来指定当前主线程等待其它线程运行完成后。再来继续运行Thread.join()后面的代码。
package com.example; import java.util.Date; import java.util.concurrent.TimeUnit; public class DataSourcesLoader implements Runnable{ @Override public void run() { System.out.printf("Beginning data sources loading: %s\n",new Date()); try { TimeUnit.SECONDS.sleep(4); } catch (InterruptedException e) { e.printStackTrace(); } System.out.printf("Data sources loading has finished: %s\n",new Date()); } public static void main(String[] args){ DataSourcesLoader dsLoader = new DataSourcesLoader(); Thread thread1 = new Thread(dsLoader,"DataSourceThread"); thread1.start(); try { thread1.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.printf("Main: Configuration has been loaded: %s\n",new Date()); } }运行结果:
Beginning data sources loading: Fri Nov 14 14:27:31 CST 2014 Data sources loading has finished: Fri Nov 14 14:27:35 CST 2014 Main: Configuration has been loaded: Fri Nov 14 14:27:35 CST 2014假设去掉thread1.join(),运行的结果例如以下:
Main: Configuration has been loaded: Fri Nov 14 14:28:33 CST 2014 Beginning data sources loading: Fri Nov 14 14:28:33 CST 2014 Data sources loading has finished: Fri Nov 14 14:28:37 CST 2014通过结果。就能够非常明显的说明上面红字的部分:“再来继续运行Thread.join()后面的代码”
package com.example; import java.util.Date; import java.util.concurrent.TimeUnit; public class DataSourcesLoader implements Runnable{ @Override public void run() { System.out.printf("Beginning data sources loading: %s\n",new Date()); try { TimeUnit.SECONDS.sleep(4); } catch (InterruptedException e) { e.printStackTrace(); } System.out.printf("Data sources loading has finished: %s\n",new Date()); } public static void main(String[] args){ DataSourcesLoader dsLoader = new DataSourcesLoader(); Thread thread1 = new Thread(dsLoader,"DataSourceThread"); thread1.start(); try { thread1.join(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.printf("Main: Configuration has been loaded: %s\n",new Date()); } }
thread1.join(3000);这句话的意思是,仅仅要满足以下2个条件中的一个时,主线程就会继续运行thread.join()后面的代码:
条件1:thread1 运行完成。
条件2:已经等待 thread1 运行了3000ms.
样例中。thread1 自身的运行时间是4s。而设置的等待时间是3s,所以得到的运行结果例如以下。thread1还没有运行完,主线程就開始运行后面的代码。由于 thread1 等待的时间已经超时了:
Beginning data sources loading: Fri Nov 14 14:35:45 CST 2014 Main: Configuration has been loaded: Fri Nov 14 14:35:48 CST 2014 Data sources loading has finished: Fri Nov 14 14:35:49 CST 2014
那么结合上面的2个样例。我们能够判断出以下代码的运行结果了:
package com.example; import java.util.Date; import java.util.concurrent.TimeUnit; public class DataSourcesLoader implements Runnable{ @Override public void run() { System.out.printf("Beginning data sources loading: %s\n",new Date()); try { TimeUnit.SECONDS.sleep(4); } catch (InterruptedException e) { e.printStackTrace(); } System.out.printf("Data sources loading has finished: %s\n",new Date()); } }
package com.example; import java.util.Date; import java.util.concurrent.TimeUnit; public class NetworkConnectionsLoader implements Runnable{ @Override public void run() { System.out.printf("Beginning network connect loading: %s\n",new Date()); try { TimeUnit.SECONDS.sleep(6); } catch (InterruptedException e) { e.printStackTrace(); } System.out.printf("Network connect loading has finished: %s\n",new Date()); } public static void main(String[] args){ DataSourcesLoader dsLoader = new DataSourcesLoader(); Thread thread1 = new Thread(dsLoader,"DataSourceThread"); NetworkConnectionsLoader ncLoader = new NetworkConnectionsLoader(); Thread thread2 = new Thread(ncLoader,"NetworkConnectionLoader"); thread1.start(); thread2.start(); try { thread1.join(); thread2.join(1900); } catch (InterruptedException e) { e.printStackTrace(); } System.out.printf("Main: Configuration has been loaded: %s\n",new Date()); } }运行结果:
Beginning data sources loading: Fri Nov 14 14:39:20 CST 2014 Beginning network connect loading: Fri Nov 14 14:39:20 CST 2014 Data sources loading has finished: Fri Nov 14 14:39:24 CST 2014 Main: Configuration has been loaded: Fri Nov 14 14:39:26 CST 2014 Network connect loading has finished: Fri Nov 14 14:39:26 CST 2014
thread2.join(3000);结果会和上面的一样吗? 依据我最開始指出的“Thread.join()。是用来指定当前主线程等待其它线程运行完成后,再来继续运行Thread.join()后面的代码。”
我们能够看到,运行结果会有区别:
Beginning data sources loading: Fri Nov 14 14:41:21 CST 2014 Beginning network connect loading: Fri Nov 14 14:41:21 CST 2014 Data sources loading has finished: Fri Nov 14 14:41:25 CST 2014 Network connect loading has finished: Fri Nov 14 14:41:27 CST 2014 Main: Configuration has been loaded: Fri Nov 14 14:41:27 CST 2014</span>
至于为什么会有这个区别,我上面也已经说明了,我想这个应该不难理解。
PS:代码部分截取来自《Java 7 Concurrency Cookbook》
版权声明:本文博客原创文章。博客,未经同意,不得转载。
标签:
原文地址:http://www.cnblogs.com/lcchuguo/p/4710057.html