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 ...
随机推荐
- ThinkPHP连接数据库出现的错误:Undefined class constant 'MYSQL_ATTR_INIT_COMMAND'
最近看了看ThinkPHP.在连接mysql数据库时出现了错误:Undefined class constant 'MYSQL_ATTR_INIT_COMMAND'.意思就是没有PDO(PHP数据对象 ...
- [初探iOS开发]storyboard的使用
storyboard的目的是为了方便的设计程序view之间的关系,使得程序员把精力都放到核心业务逻辑之上.
- C语言数据结构----递归的应用(八皇后问题的具体流程)
本节主要讲八皇后问题的基本规则和递归回溯算法的实现以及具体的代码实现和代码分析. 转载请注明出处.http://write.blog.csdn.net/postedit/10813257 一.八皇后问 ...
- POJ 2418 Hardwood Species( AVL-Tree )
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> ...
- Cocos2d-x 地图行走的实现3:A*算法
本文乃Siliphen原创,转载请注明出处:http://blog.csdn.net/stevenkylelee 上一节<Cocos2d-x 地图行走的实现2:SPFA算法>: http: ...
- linux下的开源移动图像监测程序--motion编译与配置
前几天在网上偶然看到一篇博客,是利用linxu下的开源的motion搭建嵌入式视频动态监控系统,感觉很好很强大于,是就想自己编译移植一下试试. 所谓移动图像监测,简单来说就是利用摄像头定点监测某个区域 ...
- UML简单介绍
UML的全称是统一建模语言:Unified Modeling Language. 是用来为面向对象开发系统的产品进行说明可视化和编制文档的方法. 它是一种标准的图形化建模语言,是面向对象分析与设计的一 ...
- python—webshell_醉清风xf_新浪博客
python—webshell_醉清风xf_新浪博客 python—webshell (2012-05-23 09:55:46) 转载▼
- 跨平台网络通信与server编程框架库(acl库)介绍
一.描写叙述 acl project是一个跨平台(支持LINUX,WIN32,Solaris,MacOS,FreeBSD)的网络通信库及server编程框架,同一时候提供很多其它的有用功能库.通过该库 ...
- Cocos2d-x精华教程汇总(第三期) cocos2d-x最新离线API文档下载(最新版3.6更新。。。)
其实使用doxygen在Cocos2d-x引擎的doc目录下可以生成离线文档,但是可能每个人为了生成一个离线文档去安装甚至编译doxygen毕竟麻烦,而且现有的doxygen无法生成多语言版本的离线文 ...