Thread-Join


    /**
     * Waits at most {@code millis} milliseconds for this thread to
     * die. A timeout of {@code 0} means to wait forever.
     *
     * <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.
     *
     * @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");
        }

        if (millis == 0) {
        // isAlive()判断当前子线程是否结束,如果没有结束就调用wait(0)
            while (isAlive()) {
                wait(0);
            }
        } else {
            while (isAlive()) {
                long delay = millis - now;
                if (delay <= 0) {
                    break;
                }
                wait(delay);
                now = System.currentTimeMillis() - base;
            }
        }
    }

实例

package com.example.demo.thread;

import java.util.concurrent.Callable;

public class JoinTest {
    public static void main(String[] args) throws Exception {
        JoinThread1 joinThread1 = new JoinThread1();
        JoinThread2 joinThread2 = new JoinThread2();
        JoinThread3 joinThread3 = new JoinThread3();
        joinThread1.start();
//        joinThread2.start();
//        joinThread3.start();
    }

}


class JoinThread1 extends Thread {

    public void run()  {
        /**
         * join 一定要用在start()之后,因为只有线程启动后才可以被线程调度器进行线程间通信
         */
        JoinThread2 joinThread2 = new JoinThread2();
        joinThread2.setName("joinThread2");
        joinThread2.start();
        try {
            joinThread2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("1");
    }
}
class JoinThread2 extends Thread  {

    public void run()  {
        JoinThread3 joinThread3 = new JoinThread3();
        joinThread3.setName("joinThread3");
        joinThread3.start();
        try {
            joinThread3.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("2");
    }
}
class JoinThread3 extends Thread  {
    public void run()  {
        System.out.println("3");
    }
}

##输出

3
2
1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值