任务T1、T2、T3并发执行,最后执行任务T4的实现方法

2021年9月12日面试茄子快传的时候,提到三个任务T1T2T3的执行需要并发执行,且最后执行任务T4。那么应该怎么实现。

当时有点忘记了具体的实现,只知道在Java中,如果抽象为线程任务,那么有两种机制可以用来实现:

  • Thread提供的join方法;
  • 使用JUC下面的CountDownLatch类;

这里来简单使用下这两种方法。

1. join方法来实现

等待线程执行终止。
在主线程中如果对子线程使用了join方法,那么主线程会等待join的线程执行完毕,当所有的执行完毕后,再继续向下执行主线程。

故而在当前的场景下,可以构建如下代码:

public class Main {

    private static volatile int[] arr = new int[4];

    private static class ThreadFactory{
        private static AtomicInteger number = new AtomicInteger(1);
        private ThreadFactory(){}
        public static Thread createThread(Runnable r){
            return new Thread(r, "Task#" + number.getAndIncrement());
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = ThreadFactory.createThread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 5; i++) {
                    arr[0] += 10;
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "\t" + arr[0]);
                }
            }
        });

        thread1.start();

        Thread thread2 = ThreadFactory.createThread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 5; i++) {
                    arr[1] += 20;
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "\t" + arr[1]);
                }
            }
        });

        thread2.start();

        Thread thread3 = ThreadFactory.createThread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 5; i++) {
                    arr[2] += 30;
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "\t" + arr[2]);
                }
            }
        });

        thread3.start();

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

        // Task 4默认为主线程即可
        arr[3] = arr[0] + arr[1] + arr[2];

        for (int i : arr) {
            System.out.print(i+"\t");
        }
        System.out.println();
    }

}

结果:
在这里插入图片描述
从上面结果可以看出满足了T1T2T3并发执行,最后执行任务T4的要求。

2. CountDownLatch类来实现

CountDownLatch是一个同步工具类,用来协调多个线程之间的同步。当计数器数值减为0时,所有受其影响而等待的线程将会被激活。

CountDownLatch主要两个方法就是一是CountDownLatch.await()阻塞当前线程,二是CountDownLatch.countDown()当前线程把计数器减一。

对应的实现:

public class Main {

    private static volatile int[] arr = new int[4];

    private static class ThreadFactory{
        private static AtomicInteger number = new AtomicInteger(1);
        private ThreadFactory(){}
        public static Thread createThread(Runnable r){
            return new Thread(r, "Task#" + number.getAndIncrement());
        }
    }

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(3);  // 这里需要三个并发,故而这里设置值为3

        Thread thread1 = ThreadFactory.createThread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 5; i++) {
                    arr[0] += 10;
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "\t" + arr[0]);
                }
                // 这个线程执行完毕,进行释放
                countDownLatch.countDown();
            }
        });

        thread1.start();

        Thread thread2 = ThreadFactory.createThread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 5; i++) {
                    arr[1] += 20;
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "\t" + arr[1]);
                }
                // 这个线程执行完毕,进行释放
                countDownLatch.countDown();
            }
        });

        thread2.start();

        Thread thread3 = ThreadFactory.createThread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 5; i++) {
                    arr[2] += 30;
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "\t" + arr[2]);
                }
                // 这个线程执行完毕,进行释放
                countDownLatch.countDown();
            }
        });

        thread3.start();


        // Task 4默认为主线程即可
        // 首先阻塞一下
        countDownLatch.await();
        arr[3] = arr[0] + arr[1] + arr[2];

        for (int i : arr) {
            System.out.print(i+"\t");
        }
        System.out.println();

    }

}

结果:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

梦否

文章对你有用?不妨打赏一毛两毛

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

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

打赏作者

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

抵扣说明:

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

余额充值