Thread.join()分析方法
API:
join
public final void join()
throws InterruptedException
- 等待该线程终止。
-
- 抛出:
InterruptedException
- 假设不论什么线程中断了当前线程。当抛出该异常时,当前线程的中断状态 被清除。
join
public final void join(long millis)
throws InterruptedException
- 等待该线程终止的时间最长为
millis
毫秒。超时为0
意味着要一直等下去。
-
- 參数:
millis
- 以毫秒为单位的等待时间。- 抛出:
InterruptedException
- 假设不论什么线程中断了当前线程。当抛出该异常时,当前线程的中断状态 被清除。
join
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()后面的代码。
例1:
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()后面的代码”
例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());
} 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个样例。我们能够判断出以下代码的运行结果了:
例3:
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
注意:假设把例3的 thread2.join(1900) 部分改动为:
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》
版权声明:本文博客原创文章。博客,未经同意,不得转载。
Thread.join()分析方法的更多相关文章
- 关于C#中Thread.Join()的一点理解
原文地址:http://www.cnblogs.com/slikyn/articles/1525940.html 今天是第一次在C#中接触Thread,自己研究了一下其中Thread.Join()这个 ...
- C#多线程Thread.Join()的详解
class TestThread { private static void FirstThreadFun() { ; i < ; i++) { Console.WriteLine(Thread ...
- Thread.Join()的详解
什么是进程?当一个程序开始运行时,它就是一个进程,进程包括运行中的程序和程序所使用到的内存和系统资源.而一个进程又是由多个线程所组成的. 什么是线程?线程是程序中的一个执行流,每个线程都有自己的专有寄 ...
- C#多线程详解(一) Thread.Join()的详解
bicabo C#多线程详解(一) Thread.Join()的详解 什么是进程?当一个程序开始运行时,它就是一个进程,进程包括运行中的程序和程序所使用到的内存和系统资源.而一个进程又是由多个线程 ...
- Thread.join()方法
thread.Join把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程.比如在线程B中调用了线程A的Join()方法,直到线程A执行完毕后,才会继续执行线程B.t.join() ...
- Java Thread.join()方法
一.使用方式. join是Thread类的一个方法,启动线程后直接调用,例如: Thread t = new AThread(); t.start(); t.join(); 二.为什么要用join() ...
- JAVA THREAD.JOIN方法详解
一.使用方式. join是Thread类的一个方法,启动线程后直接调用,例如: Thread t = new AThread(); t.start(); t.join(); 二.为什么要用join() ...
- Thread.Join 和 Task.Wait 方法
这两个方法 可以说是类似的功能,都是对当前任务进行等待阻塞,执行完毕后再进行后续处理 talk is cheap, show you code,下面一个是异步执行,一个是加了阻塞,可以对比不同执行结果 ...
- 多线程--Thread.join方法
在Thread类的Api中,Join的作用是让当前线程等待目标线程结束之后才继续执行. thread.Join把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程. 比如在线程B ...
随机推荐
- linux-sfdisk 使用方法
功能说明:硬盘分区工具程序. 语 法:sfdisk [-?Tvx][-d <硬盘>][-g <硬盘>][-l <硬盘>][-s <分区>][-V < ...
- Android 进行单元測试难在哪-part3
原文链接 : HOW TO MAKE OUR ANDROID APPS UNIT TESTABLE (PT. 1) 原文作者 : Matthew Dupree 译文出自 : 开发技术前线 www.de ...
- [初探iOS开发]storyboard的使用
storyboard的目的是为了方便的设计程序view之间的关系,使得程序员把精力都放到核心业务逻辑之上.
- CF 316C2(Tidying Up-二分图最大边权)
C2. Tidying Up time limit per test 4 seconds memory limit per test 256 megabytes input standard inpu ...
- POJ 2318 TOYS(计算几何)
跨产品的利用率推断点线段向左或向右,然后你可以2分钟 代码: #include <cstdio> #include <cstring> #include <algorit ...
- JAVA进阶----主线程等待子线程各种方案比较(转)
创建线程以及管理线程池基本理解 参考原文链接:http://www.oschina.net/question/12_11255?sort=time 一.创建一个简单的java线程 在 Java 语言中 ...
- OCP读书笔记(10) - 使用闪回技术I
使用闪回技术查询数据 闪回查询:就是查询表在过去某个时间点的数据,所用到的技术就是undo数据 SQL> conn scott/tiger 创建测试表 SQL> create table ...
- Java NIO的性能
最近调研了一下mina和netty框架的性能,主要是想了解java nio在单机能支持多少长连接. 首先,mina的qq群有同学反映说单机支持3w长连接是没问题的 其次,http://amix.dk/ ...
- 基于Cocos2dx开发卡牌游戏Demo_放开那三国 2.0
PS:下载地址在最以下 1.登录 2.副本选择 3.地图 4. 选择敌人 5. 战斗 6. 战斗结算 7. 地图拓展 8. 武将拓展 9. 下载地址: 点击打开链接
- Visual Prolog 的 Web 专家系统 (8)
GENI核心 -- 推理引擎(2)流量控制 1.阐述fail."!"而回溯 与其他语言相比,,Prolog最大的特点.这是回溯机制. 回溯机制,还有的主要手段2个月,首先,通过使用 ...