join
join
join是Thread方法,它的作用是A线程中子线程B在运行之后调用了B.join(),A线程会阻塞直至B线程执行结束
join源码(只有继承Thread类才能使用)
基于openjdk1.8的源码
public final void join() throws InterruptedException {
join(0);
}
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) {
while (isAlive()) {
wait(0);
}
} else {
while (isAlive()) {
long delay = millis - now;
if (delay <= 0) {
break;
}
wait(delay);
now = System.currentTimeMillis() - base;
}
}
}
/**
* Tests if this thread is alive. A thread is alive if it has
* been started and has not yet died.
*
* @return <code>true</code> if this thread is alive;
* <code>false</code> otherwise.
*/
public final native boolean isAlive();
/* <p>
* Note that the {@code wait} method, as it places the current thread
* into the wait set for this object, unlocks only this object; any
* other objects on which the current thread may be synchronized remain
* locked while the thread waits.
* <p>
...
*/
public final native void wait(long timeout) throws InterruptedException;
源码分析
A线程调用了B.join(),获取了B的锁,当B alive,B.wait(0)会让当前线程A阻塞,执行join方法等同于,A线程进入了下列
的语句
syncronized(B){
...
B.wait
...
}
代码测试
package com.java.javabase.thread.base;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class JoinTest {
public static void main(String[] args) {
Thread t1 =new ThreadOne("t1");
t1.start();
log.info("current thread is : {} run",Thread.currentThread().getName());
try {
t1.join();
} catch (InterruptedException e) {
log.info("InterruptedException",e);
e.printStackTrace();
}
log.info("current thread is : {} end",Thread.currentThread().getName());
}
static class ThreadOne extends Thread{
public ThreadOne(String name){
super(name);
}
@Override
public void run(){
log.info("current thread is : {} start",Thread.currentThread().getName());
for(int i =0;i<10;i++)
{
log.info("current thread is : {} run",Thread.currentThread().getName());
}
log.info("current thread is : {} end",Thread.currentThread().getName());
}
}
}
说明
主线程调用t1.join之后,主线程只有t1的锁进入阻塞状态
运行结果
2019-07-29 20:14:21,551 [t1] INFO JoinTest - current thread is : t1 start
2019-07-29 20:14:21,551 [t1] INFO JoinTest - current thread is : t1 run
2019-07-29 20:14:21,551 [t1] INFO JoinTest - current thread is : t1 run
2019-07-29 20:14:21,551 [t1] INFO JoinTest - current thread is : t1 run
2019-07-29 20:14:21,551 [t1] INFO JoinTest - current thread is : t1 run
2019-07-29 20:14:21,551 [t1] INFO JoinTest - current thread is : t1 run
2019-07-29 20:14:21,551 [t1] INFO JoinTest - current thread is : t1 run
2019-07-29 20:14:21,551 [t1] INFO JoinTest - current thread is : t1 run
2019-07-29 20:14:21,551 [t1] INFO JoinTest - current thread is : t1 run
2019-07-29 20:14:21,551 [t1] INFO JoinTest - current thread is : t1 run
2019-07-29 20:14:21,551 [t1] INFO JoinTest - current thread is : t1 run
2019-07-29 20:14:21,551 [t1] INFO JoinTest - current thread is : t1 end
2019-07-29 20:14:21,551 [main] INFO JoinTest - current thread is : main run
2019-07-29 20:14:21,551 [main] INFO JoinTest - current thread is : main end