??
?????一个线程可以在其他线程上调用join()方法,其效果是等待一段时间直到第二个线程结束才能继续执行。
?
/**
* Waits for this thread to die.
*
* <p> An invocation of this method behaves in exactly the same
* way as the invocation
*
* <blockquote>
* {@linkplain #join(long) join}{@code (0)}
* </blockquote>
*
* @throws InterruptedException
* if any thread has interrupted the current thread. The
* <i>interrupted status</i> of the current thread is
* cleared when this exception is thrown.
*/
public final void join() throws InterruptedException {
join(0);
}
?
/**
*
* @author zhangwei_david
* @version $Id: One.java, v 0.1 2015年5月18日 上午10:40:37 zhangwei_david Exp $
*/
public class One extends Thread {
/**
* @see java.lang.Thread#run()
*/
@Override
public void run() {
System.out.println(super.getName() + " is running....");
System.out.println(super.getName() + " end.");
}
public One(String name) {
super(name);
}
}
?
/**
*
* @author zhangwei_david
* @version $Id: Two.java, v 0.1 2015年5月18日 上午10:41:41 zhangwei_david Exp $
*/
public class Two extends Thread {
private Thread one;
/**
* @see java.lang.Thread#run()
*/
@Override
public void run() {
System.out.println(" two is running....");
one.start();
try {
one.join();
} catch (InterruptedException e) {
}
System.out.println(" two end");
}
public Two(Thread one) {
super();
this.one = one;
}
}
?
/**
*
* @author zhangwei_david
* @version $Id: JoinDemo.java, v 0.1 2015年5月17日 下午3:32:45 zhangwei_david Exp $
*/
public class JoinDemo {
/**
*
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
Thread one = new One("One");
Thread two = new Two(one);
two.start();
}
}
?从结果中可以发现在线程Two?被挂起,直到线程One执行完成后才继续执行。
two is running.... One is running.... One end. two end
?
??那么join?是如何实现的呢?我们可以看看源码。
public final synchronized void join(long millis)
throws InterruptedException {
// 获取当前时间
long base = System.currentTimeMillis();
long now = 0;
// 超时时间小于0
if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
// 超时时间等于0
if (millis == 0) {
//判断当前线程是否是启动的,如果是启动的一直等待到线程的执行结束也就是没有超时时间
while (isAlive()) {
// 如果是启动的则等待0毫秒
wait(0);
}
// 如果超时时间大于0
} else {
// 当前线程是启动状态,则一直等待到线程执行结束或者超时
while (isAlive()) {
// 获取延迟时间
long delay = millis - now;
// 延迟时间小于或等于0 则不需要等待
if (delay <= 0) {
break;
}
// 等待 delay 毫秒
wait(delay);
// 设置now 值
now = System.currentTimeMillis() - base;
}
}
}
?
/** *一直等待到其他线程notify(),notifyAll()或者是超时 * **/ public final native void wait(long timeout) throws InterruptedException;
?
?
原文:http://zhangwei-david.iteye.com/blog/2212123