Thread的join方法

Thread中的join方法主要的作用是让jion的线程加入当前线程,等加入的线程执行完之后才会执行当前线程。

接下来看个例子:

public class TestJoin {

    public static void main(String[] args) throws InterruptedException {



        Thread t1 = new Thread(() ->
        {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("this is t1");
        });

        Thread t2 = new Thread(() -> {

            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("this is t2");
        });


        Thread t3 = new Thread(() -> {
            System.out.println("this is t3");
        });

        t1.start();
        t2.start();
        t3.start();
        t1.join();
        t2.join();
        t3.join();
        System.out.println("运行结束");
    }

}

输出结果为:

this is t3
this is t1
this is t2
运行结束

可以看到输出是线程t1,t2,t3输出之后,才会输出到主线程。所以可以看出来join方法是把其他线程加入当前线程,等加入线程执行完之后才会执行当前线程。

如果想要t1,t2,t3再顺序输出,只需要在每个start方法后面再调用join就行了。这就就保证了每次主线程运行完一个线程,再去运行另外一个线程。

修改上面代码为:

public class TestJoin {

    public static void main(String[] args) throws InterruptedException {


        Thread t1 = new Thread(() ->
        {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("this is t1");
        });

        Thread t2 = new Thread(() -> {

            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("this is t2");
        });


        Thread t3 = new Thread(() -> {
            System.out.println("this is t3");
        });

        t1.start();
        t1.join();
        t2.start();
        t2.join();
        t3.start();
        t3.join();
        System.out.println("运行结束");
    }

}

输出为:

this is t1
this is t2
this is t3
运行结束

 

接下来我们再从源码分析下Thread的join方法是怎么实现的:

    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;
            }
        }
    }

 

由源码可以看到,其实底层就是wait方法,并且有一个while死循环,直到isAlive是False,也就是线程执行完成或者不存活。

顺带一提下Thread的yield的方法,此方法其实是只当前线程让出CPU回到就绪状态,线程再重新争抢CPU,但是结果可能是其他线程抢到CPU,也可能是自己抢到CPU。

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值