首页 技术 正文
技术 2022年11月17日
0 收藏 800 点赞 3,968 浏览 4744 个字

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》

版权声明:本文博客原创文章。博客,未经同意,不得转载。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,557
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898