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 ...
随机推荐
- POJ 1159 - Palindrome 优化空间LCS
将原串和其逆序串的最长公共子序列求出来为M..那么2*n-M就是所需要加的最少字符..因为求出的M就是指的原串中"潜伏"的最长回文.. 问题转化为求LCS..但是n最大到5000. ...
- Spring常见面试问题 (转)
Spring 1. Spring工作机制及为什么要用?Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的.Spring既是一个AOP框架,也是一IOC容器.SpringFramew ...
- JavaScript引用类型之Object类
ECMAScript中的Object类跟Java中的Object类相似,ECMAScript中的全部类都由这个类继承而来,Object类中的全部属性和方法都会出如今其他类中,所以理解Object类,就 ...
- hdu 1392(凸包)
传送门:Surround the Trees 题意:求凸包的周长. 分析:凸包模板题,先按极角排好序后,然后根据叉积正负确定凸包. #include <stdio.h> #include ...
- ArcGIS 10.2 操作SQLite
SQLite,是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它.ArcGIS 10.2 提供了对SQLite数据库的支持,这对那 ...
- solr4.9r+ Eclipse 4.3+ tomcat 7.5 +winds7(二)
尊重原创,原文地址:http://blog.csdn.net/chunlei_zhang/article/details/38778945 这另外一种方法是将solr项目部署到tomcat上,执行to ...
- 开源 免费 java CMS - FreeCMS1.9 会员组管理
项目地址:http://www.freeteam.cn/ 会员组管理 会员组分为两种,一级是经验会员组,一种是特殊会员组. 经验会员组的会员会依据经验自己主动变更,特殊会员组不会自己主动变更,须要管理 ...
- Android_模拟时钟内时针、分针触摸转动
最近实现了android里的一个机能,在activity里面画了一个模拟的时针,然后触摸上面的时针跟分针可以实现调时间的功能. 其实,说起原来来还是挺简单的,但是我花了将近一周的时间才全部实现,有点惭 ...
- MYSQL查询一周内的数据(最近7天的)、最近一个月、最近三个月数据
如果你要严格要求是某一年的,那可以这样 查询一天: select * from table where to_days(column_time) = to_days(now()); select * ...
- 通过Java字节码发现有趣的内幕之String篇(上)(转)
原文出处: jaffa 很多时候我们在编写Java代码时,判断和猜测代码问题时主要是通过运行结果来得到答案,本博文主要是想通过Java字节码的方式来进一步求证我们已知的东西.这里没有对Java字节码知 ...