Java 有两个任务A与B,多线程执行任务A完成后再继续执行任务B

内容描述:

在多线程中,有两个任务A与B,多线程执行任务A完成后再继续执行任务B,想到的两种方法一种线程间通信,使用条件变量方式,第二种就是使用CountDownLatch。

条件变量

主要使用的条件变量,判断是否任务A已经做完,然后再执行任务B
public class Main {

    public static boolean[] flag = new boolean[3];

    public static boolean judge(boolean[] f) {
        for (int i = 0; i < f.length; i++) {
            if (!f[i]) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        for (int i = 0; i < flag.length; i++) {
            flag[i] = false;
        }
        new Thread(new Runnable() {
            public void run() {
                while (true) {
                    if (!flag[0]) {
                        System.out.println("T1: work A");
                        flag[0] = true;
                    }
                    if (judge(flag)) {
                        System.out.println("T1: work B");
                        break;
                    }                   
                }
            }
        }).start();
        new Thread(new Runnable() {
            public void run() {
                while (true) {
                    if (!flag[1]) {
                        System.out.println("T2: work A");
                        flag[1] = true;
                    }
                    if (judge(flag)) {
                        System.out.println("T2: work B");
                        break;
                    }                   
                }               
            }
        }).start();

        new Thread(new Runnable() {
            public void run() {
                while (true) {
                    if (!flag[2]) {
                        System.out.println("T3: work A");
                        flag[2] = true;
                    }
                    if (judge(flag)) {
                        System.out.println("T3: work B");
                        break;
                    }                   
                }
            }
        }).start();
    }
}

运行结果:
这里写图片描述

使用CountDownLatch

我们也可以使用CountDownLatch,然后通过CountDown()与await()方法进行线程运行与等待。
import java.util.concurrent.CountDownLatch;


public class Main {



    public static void main(String[] args) throws InterruptedException{
       CountDownLatch countDownLatch = new CountDownLatch(3);
       new Thread(new Runnable() {
           public void run() {
            System.out.println("aWork:  A ");
            countDownLatch.countDown();
            try {
                countDownLatch.await();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("aWork:  B");
           }
       }).start();
       new Thread(new Runnable() {
           public void run() {
            System.out.println("bWork:  A ");
            countDownLatch.countDown();
            try {
                countDownLatch.await();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("bWork:  B");
           }
       }).start();
       new Thread(new Runnable() {
           public void run() {
            System.out.println("cWork:  A ");
            countDownLatch.countDown();
            try {
                countDownLatch.await();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("cWork:  B");
           }

       }).start();


    }
}

这里写图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值