多线程join分析

多线程下join的应用与分析

一、场景:多线程环境下
二、需求:假如有两个线程,如何保证线程的顺序执行
三、解决方案:使用join的方式
四、原理分析

因此就引入了join的使用,当然保证线程的顺序执行肯定不止join这一种,本文主要是为了说明join的应用

案例一、

public class ThreadA extends Thread {

    @Override
    public void run() {
        for (int i = 0; i < 5000; i++) {
            System.out.println("线程名称:" + Thread.currentThread().getName());
        }
    }
}
public class ThreadB extends  Thread {

    @Override
    public void run() {
        for (int i = 0; i < 5000; i++) {
            System.out.println("线程名称:" + Thread.currentThread().getName());
        }
    }
}
public class JoinThreadDemo1 {

    public static void main(String[] args) {
        Thread threadA = new Thread(new ThreadA(), "ThreadA");
        Thread threadB = new Thread(new ThreadB(), "ThreadB");
        threadA.start();
        threadB.start();
    }

}

运行结果:

线程名称:ThreadA
线程名称:ThreadA
线程名称:ThreadA
线程名称:ThreadA
线程名称:ThreadB
线程名称:ThreadB
线程名称:ThreadB
线程名称:ThreadB
线程名称:ThreadB
线程名称:ThreadB
线程名称:ThreadB
线程名称:ThreadB
线程名称:ThreadA
...

线程会交替执行

那么加入join后

    public static void main(String[] args) throws InterruptedException {
        Thread threadA = new Thread(new ThreadA(), "ThreadA");
        Thread threadB = new Thread(new ThreadB(), "ThreadB");
        threadA.start();
        threadA.join();
        threadB.start();
    }

结果就是按照先执行完ThreadA,在执行ThreadB,这里其实还有个有意思的地方,你会发现这里抛出了一个InterruptedException,说明join是阻塞的,通过代码分析,你会发现凡是阻塞式都会抛出一个InterruptedException异常。言归正传,继续join

这里我们先来说下,为什么加入了join就会先执行完线程A,在执行线程B?

原因是join方法是在main主线程上调用的,在调用完后会让main主线程暂时挂起,当threadA执行完成以后唤醒main主线程。

那么这里有个疑问为什么使用join会导致main线程阻塞呢

Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. 

直译的意思是:导致当前线程等待,直到另一个线程为该对象调用notify()方法或notifyAll()方法。当前线程指的是当前CPU正在执行的线程,在上述例子中main线程是当前正在执行线程,而threadA正好又是同步对象,所以在main线程中调用threadA.join()实际是使main线程进入了等待。

   /**
     * 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) {
            while (isAlive()) {
                wait(0);
            }
        } else {
            while (isAlive()) {
                long delay = millis - now;
                if (delay <= 0) {
                    break;
                }
                wait(delay);
                now = System.currentTimeMillis() - base;
            }
        }
    }

可以看出,join()方法的底层是利用wait()方法实现的。可以看出,join方法是一个同步方法,当主线程调用threadA.join()方法时,主线程先获得了threadA对象的锁,随后进入方法,调用了threadA对象的wait()方法,使主线程进入了threadA对象的等待池,此时,threadA线程则还在执行,并且随后的threadB.start()还没被执行,因此,threadB线程也还没开始。等到threadA线程执行完毕之后,主线程继续执行,走到了threadB.start(),threadB线程才会开始执行

参考:https://blog.csdn.net/weixin_43184769/article/details/89599632

​ https://blog.csdn.net/u013425438/article/details/80205693

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值