1.join()解释:
自我理解:
Waits for this thread to die.(等待该线程{xxx.join()的xxx线程}执行结束后,当前线程再开始执行)换句话说就是谁.join()就等谁执行。
网上解释:join()是Thread类的一个方法,根据jdk文档的定义,join()方法的作用,是等待这个线程结束,即当前线程等待另一个调用join()方法的线程执行结束后再往下执行。通常用于在main主线程内,等待其它调用join()方法的线程执行结束再继续执行main主线程。(来源:https://www.jianshu.com/p/5d88b122a050)
2.应用实例:
需求:
从几台服务器上采集数据,并将它们的采集的结果存入数据库
(1).不使用join()
package com.springboot.thread;
/*
* join方法:join()是Thread类的一个方法,根据jdk文档的定义,
* join()方法的作用,是等待这个线程结束,即当前线程等待另一个
* 调用join()方法的线程执行结束后再往下执行。
* 通常用于在main主线程内,等待其它调用join()方法的线程执行结束再继续执行main主线程。
* */
public class ThreadJoinDemo {
/*需求模拟:从几台服务器上采集数据,并将它们的采集的结果存入数据库*/
public static void main(String[] args) {
System.out.println("开始收集");
Thread thread1 = new Thread(new ComputerRunable("M1",1000l));
Thread thread2 = new Thread(new ComputerRunable("M2",2000L));
Thread thread3 = new Thread(new ComputerRunable("M3",3000L));
thread1.start();
thread2.start();
thread3.start();
System.out.println("收集结束");
}
}
class ComputerRunable implements Runnable{
private String computerName;
private Long spentTime;
public ComputerRunable(String computerName,Long spentTime) {
this.computerName = computerName;
this.spentTime = spentTime;
}
@Override
public void run() {
/*采集数据*/
try {
Thread.sleep(spentTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(computerName+"数据收集完毕!");
}
}
运行结果:
开始收集
收集结束
M1数据收集完毕!
M2数据收集完毕!
M3数据收集完毕!
可以看出:我们还没有开始收集,主线程就已经结束了。
(2).使用join()
package com.springboot.thread;
/*
* join方法:join()是Thread类的一个方法,根据jdk文档的定义,
* join()方法的作用,是等待这个线程结束,即当前线程等待另一个
* 调用join()方法的线程执行结束后再往下执行。
* 通常用于在main主线程内,等待其它调用join()方法的线程执行结束再继续执行main主线程。
* */
public class ThreadJoinDemo {
/*需求模拟:从几台服务器上采集数据,并将它们的采集的结果存入数据库*/
public static void main(String[] args) throws InterruptedException {
System.out.println("开始收集");
Thread thread1 = new Thread(new ComputerRunable("M1",1000l));
Thread thread2 = new Thread(new ComputerRunable("M2",2000L));
Thread thread3 = new Thread(new ComputerRunable("M3",3000L));
thread1.start();
thread2.start();
thread3.start();
thread1.join();
thread2.join();
thread3.join();
System.out.println("收集结束");
}
}
class ComputerRunable implements Runnable{
private String computerName;
private Long spentTime;
public ComputerRunable(String computerName,Long spentTime) {
this.computerName = computerName;
this.spentTime = spentTime;
}
@Override
public void run() {
/*采集数据*/
try {
Thread.sleep(spentTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(computerName+"数据收集完毕!");
}
}
运行结果:
开始收集
M1数据收集完毕!
M2数据收集完毕!
M3数据收集完毕!
收集结束
从运行结果来看:符合我们的预期。
3.join的实现原理:
public final synchronized void join(long millis) throws InterruptedException {
long base = System.currentTimeMillis();
long now = 0;
if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
if (millis == 0) { //判断是否携带阻塞的超时时间,等于0表示没有设置超时时间
while (isAlive()) {//isAlive获取线程状态,无线等待直到previousThread线程结束
wait(0); //调用Object中的wait方法实现线程的阻塞
}
} else { //阻塞直到超时
while (isAlive()) {
long delay = millis - now;
if (delay <= 0) {
break;
}
wait(delay);
now = System.currentTimeMillis() - base;
}
}
}
解析:从join方法的源码来看,join方法的本质调用的是Object中的wait方法实现线程的阻塞,wait方法的实现原理我们在后续的文章再说详细阐述。但是我们需要知道的是,调用wait方法必须要获取锁,所以join方法是被synchronized修饰的,synchronized修饰在方法层面相当于synchronized(this),this就是previousThread本身的实例。(来源:https://www.jianshu.com/p/fc51be7e5bc0)
4.参考资料:
(1).https://www.jianshu.com/p/5d88b122a050
(2).https://www.jianshu.com/p/fc51be7e5bc0【重点推荐】