多线程的执行顺序问题

Thread类的JOIN方法

适用于线程较少的场景。
实例代码:

public class JoinOfThread {
    public static void main(String[] args) throws Exception {
        Thread threadA = new Thread(() -> {
            System.out.println("我第一个执行");
        });

        Thread threadB = new Thread(() -> {
            System.out.println("我第二个执行");
        });

        Thread threadC = new Thread(() -> {
            System.out.println("我最后执行");
        });

        threadA.start();
        threadA.join();
        threadB.start();
        threadC.join();
        threadC.start();
        
//      第二种join方法       
//        Thread threadB = new Thread(() -> {
//            try {
//                threadA.join();
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            System.out.println("我第二个执行");
//        });
//
//        Thread threadC = new Thread(() -> {
//            try {
//                threadB.join();
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            System.out.println("我最后执行");
//        });
//
//        threadA.start();
//        threadB.start();
//        threadC.start();

        
    }
}

利用volatile实现多线程的有序执行

volatile的含义介绍: volatile.

public class Demo02 {

    static volatile int order = 0;

    public static void main(String[] args) {

        Thread threadA = new Thread(() -> {
            while (true){
                if (order == 0){
                    System.out.println("我第一个执行");
                    order++;
                }
                // 只循环一次
                break;
            }
        });

        Thread threadB = new Thread(() -> {
            while (true){
                if (order == 1){
                    System.out.println("我第二个执行");
                    order++;
                    break;
                }
            }
        });

        Thread threadC = new Thread(() -> {
            while (true){
                if (order == 2){
                    System.out.println("我最后执行");
                    order = 0;
                    break;
                }
            }
        });

        threadA.start();
        threadB.start();
        threadC.start();
    }
}

synchronized + wait + notify

public class Demo03{

    public static void main(String[] args) {
        final Object synObj = new Object();
        final Object synObj2 = new Object();

        Thread t1 = new Thread(() -> {
                synchronized(synObj) {
                    try {
                        synObj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("我最后执行");
                }
        });
        t1.start();

        Thread t2 = new Thread(() -> {
                synchronized(synObj) {
                    synchronized (synObj2){
                        try {
                            synObj2.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("我第二个执行");
                    }
                    synObj.notify();
                }
        });
        t2.start();

        Thread t3 = new Thread(() -> {
                synchronized(synObj2) {
                    System.out.println("我第一个执行");
                    synObj2.notify();
                }
        });
        t3.start();
    }
}

Cadition+Lock

public class Test {
    public static Lock lock = new ReentrantLock();
    public static int count = 0;
     
    public static Condition conditionA = lock.newCondition();
    public static Condition conditionB = lock.newCondition();
     
    public static void main(String[] args) {
        Thread t1 = new Thread() {
            @Override
            public void run() {
                lock.lock();
                if (count < 5) {
                    System.out.println("线程1未达到业务要求,暂停中,等待线程2处理到达到要求后唤醒");
                    try {
                        conditionA.await();// 暂停线程并释放锁
                        System.out.println("conditionA被唤醒");
                        conditionB.await();
                        System.out.println("conditionB被唤醒");
                        System.out.println("我是线程1后面的代码");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                lock.unlock();
            }
        };
         
        Thread t2 = new Thread() {
            @Override
            public void run() {
                lock.lock();
                while (count < 10) {
                    count++;
                    System.out.println("线程2业务处理中: " + count);
                    try {
                        Thread.sleep(1000);
                        if (count == 5) {
                            conditionA.signal();
                            System.out.println("唤醒线程1");
                            lock.unlock();// 调用signal()方法后,线程2并不会释放锁,需要手动释放线程2才会执行
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                try {
                    lock.lock();// 不加这个会报java.lang.IllegalMonitorStateException
                    System.out.println("等待3秒后conditionB会被唤醒");
                    Thread.sleep(3000);
                    conditionB.signal();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                lock.unlock();// 这里释放锁,线程2执行完,线程1才会执行
            }
        };
         
        t1.start();
        t2.start();
    }
}

CountDownLatch

public class Demo04 {
    static CountDownLatch count = new CountDownLatch(1);

    public static void main(String[] args) {

        Thread thread2 = new Thread(() -> {
            try {
                count.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("1");
        });
        thread2.start();

        Thread thread1 = new Thread(() -> {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("0");
            count.countDown();
        });
        thread1.start();
    }
}

参考文档:
https://www.cnblogs.com/yuarvin/p/14506437.html

https://blog.csdn.net/qq_33709582/article/details/121900989?utm_medium=distribute.pc_category.none-task-blog-hot-2.nonecase&depth_1-utm_source=distribute.pc_category.none-task-blog-hot-2.nonecase

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值