Thread join 方法
表示等待线程的结束. 但是到底是谁等待谁呢?
是 main 主线程等待线程 t 执行完毕再执行.
下面看个例子.
- package com.citi.test.mutiplethread.demo0503;
- import java.util.Date;
- public class TestJoin implements Runnable{
- private String name;
- public TestJoin(String name) {
- this.name=name;
- }
- @Override
- public void run() {
- System.out.println("start:"+name+new Date());
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println("end:"+name+new Date());
- }
- public static void main(String[] args) throws InterruptedException {
- Thread thread1=new Thread(new TestJoin("张三"));
- Thread thread2=new Thread(new TestJoin("李四"));
- thread1.start();
- thread2.start();
- thread1.join();
- thread2.join();
- System.out.println("main thread is finished");
- }
- }
可以看到 main 线程会等待 thread1 和 thread2 线程执行完毕再执行.
如果没有 thread1.join(), thread2.join(), 那么结果会是这样.
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);
- }
join(long millis) 方法
- /**
- * Waits at most {@code millis} milliseconds for this thread to
- * die. A timeout of {@code 0} means to wait forever.
- *
- 当 millis 是 0 时, 表示永远等待
- * <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.
- *
- 这里实现用了一个 isAlive 作为循环条件. 因为一个线程停止, notifyAll 方法被调用.
- 它要求应用不用 wait,notify, 或者 notifyAll 在线程实例上.
- * @param millis
- * the time to wait in milliseconds
- *
- * @throws IllegalArgumentException
- * if the value of {@code millis} is negative
- *
- * @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 synchronized void join(long millis)
- throws InterruptedException {
- long base = System.currentTimeMillis();
- long now = 0;
- if (millis <0) {
- throw new IllegalArgumentException("timeout value is negative");
- }
- // 当调用 t.join() 方法时, join 会调用 join(0), 所以这里的 millis 是 0,
- if (millis == 0) {
- // 这里的 isAlive 是表示线程 t, 而不是 main 线程是否为 alive 状态.
- // 所以这里如果线程 t 执行完毕了, 那么 t 就不是 alive 状态了, 所以就不等待了.
- while (isAlive()) {
- // 而这里的 wait 方法, 到底是 t 线程等待还是 main 线程等待呢? 这里说实话有点疑问, 明天我研究一下 wait 到底是怎么工作的并且是什么原理呢? 到底是怎么等待.
- // 答案是 main 线程. 就是调用 t.join() 的线程是处于等待状态.
- // 这样就说通了, 当在 main 方法中调用 t.join() 方法时, 是 main 线程等待 t 线程执行完毕再执行.
- wait(0);
- }
- } else {
- while (isAlive()) {
- long delay = millis - now;
- if (delay <= 0) {
- break;
- }
- wait(delay);
- now = System.currentTimeMillis() - base;
- }
- }
- }
isAlive 方法
- /**
- * Tests if this thread is alive. A thread is alive if it has
- * been started and has not yet died.
- 测试线程是否是 alive 的状态.
- 一个线程如果已经开始了, 并且没有死掉, 那么这个线程就是 alive 状态.
- *
- * @return <code>true</code> if this thread is alive;
- * <code>false</code> otherwise.
- */
- public final native boolean isAlive();
参考资料:
来源: http://www.bubuko.com/infodetail-3230108.html