你真的理解线程是如何保证执行顺序的问题吗?

1.概述

1.我们这里的问题,主要是在同一个主线程当中,如何保证各个线程的执行顺序;
  这里我们主要是通过join方法去实现
2.下面我们先对join方法的源码进行解析一下

2.Join方法解析

2.1.概述

1.源码我们调用join方法之后,最终会调用wait(0)0表示一直等待,直到线程处理结束)
  这里注意下,我们一般调用wait之后,需要通过notify或者notifyAll方法去通知已经解锁
  这里的join方法中,C++底层实际上已经调用notifyAll我们不做过多了解;
public final void join() throws InterruptedException {
        join(0);//调用下面的join方法,下面的方法是一个线程安全的方法,锁住的对象是this 当前执行的对象
}

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

3.通过join方法实现线程顺序执行案例

3.1.未使用join方法前

code

package com.gaoxinfu.demo.jdk.rt.java.lang.thread;

/**
 * @Description:
 * @Author: gaoxinfu
 * @Date: 2020-05-17 11:38
 */
public class JoinDemo {
    public static void main(String[] args) throws InterruptedException {

        Thread thread1=new Thread(()->{
            System.out.println("thread 1");
        });
        Thread thread2=new Thread(()->{
            System.out.println("thread 2");
        });
        Thread thread3=new Thread(()->{
            System.out.println("thread 3");
        });

        thread1.start();
        thread2.start();
        thread3.start();

    }
}

执行结果

com.gaoxinfu.demo.jdk.rt.java.lang.thread.JoinDemo
thread 1
thread 3
thread 2

Process finished with exit code 0

会出现执行顺序不一致的情况

3.2.使用join方法后

package com.gaoxinfu.demo.jdk.rt.java.lang.thread;

/**
 * @Description:
 * @Author: gaoxinfu
 * @Date: 2020-05-17 11:38
 */
public class JoinDemo {
    public static void main(String[] args) throws InterruptedException {

        Thread thread1=new Thread(()->{
            System.out.println("thread 1");
        });
        Thread thread2=new Thread(()->{
            System.out.println("thread 2");
        });
        Thread thread3=new Thread(()->{
            System.out.println("thread 3");
        });

        thread1.start();
        thread1.join();
        thread2.start();
        thread2.join();
        thread3.start();

    }
}

这里不会出现执行乱序

3.3.执行流程

在这里插入图片描述
这里注意下synchronized void join锁定的对象是主线程JoinDemo

1.join方法的调用实际上阻塞住了主线程,因此,保证了线程的执行顺序
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东山富哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值