join方法
合并线程?怎末实现?
public final synchronized void join()
public final synchronized void join(long millis)
public static void main(String[] args) {
// run类实现了runnable接口
Run r = new Run();
Thread t1 = new Thread(r, "t1");
try {
t1.start();
// t1.join(); // 相当于t1.join(0); main线程等到t1执行完再往下执行
// main线程暂停直到200ms或t1运行结束
t1.join(200);
System.out.println("t1 end");
} catch(Exception e) {
e.printStackTrace();
}
System.out.println("main end");
}
class Run2 implements Runnable{
@Override
public void run() {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(200);
System.out.println(Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
它就是提供了这么一个功能,具体实现看不懂,有native方法
// 其中的wait()方法为native方法,正是Object类的wait()方法
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;
}
}
}
现在又有一个疑问了,如果是普通对象调用wait()方法,那么访问该对象的线程wait,如果是线程对象调用wait()方法,那么谁wait()呢?main线程?
糊涂了,糊涂了。wait方法和对象无关,和访问对象的线程有关,访问Thread.wait()方法的是main线程,所以wait的是main线程。
既然这样就好理解了
join方法阻塞调用方法的线程,这样就造成了线程合并的假象。实质上就是一个线程的wait。
那么,main线程wait了,怎末没有notify呢?
看了文档才知道
* <p> This implementation uses a loop of {@code this.wait} calls
* conditioned on {@code this.isAlive}. As a thread terminates the
* {@code this.notifyAll} method is invoked. It is recommended that
* applications not use {@code wait}, {@code notify}, or
* {@code notifyAll} on {@code Thread} instances.
join方法调用结束时,notifyAll方法自动被调用。
这样,main线程又能继续执行了。