【并发编程】- CountDownLatch 多线程集合同步点

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第13天,点击查看活动详情

裁判在等全部的运动员到来

实现裁判员要等待所有运动员全部到来的效果,主要处理好多个线程与同步点间阻塞的特性,线程必须都到达同步点后才可以继续向下运行。

创建线程代码如下:

public class Athletes implements Runnable {

    private CountDownLatch maxRunner;

    public Athletes(CountDownLatch maxRunner){
        super();
        this.maxRunner=maxRunner;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(2000);
            maxRunner.countDown();
            System.out.println("运动员"+Thread.currentThread().getName()+"准备好了!");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
复制代码

运行类代码如下:

public class AthletesMatch {
    public static void main(String[] args) {
        CountDownLatch maxRunner = new CountDownLatch(10);
        Athletes[] athletes = new Athletes[Integer.valueOf(String.valueOf(maxRunner.getCount()))];
        for (int i = 0; i <athletes.length ; i++) {
            athletes[i] = new Athletes(maxRunner);
            Thread thread = new Thread(athletes[i]);
            thread.setName(String.valueOf(i+1));
            thread.start();
        }
        try {
            maxRunner.await();
            System.out.println("运动员全部都准备好了,开始比赛...");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
复制代码

运行结果如下:

运动员8准备好了!
运动员3准备好了!
运动员5准备好了!
运动员6准备好了!
运动员1准备好了!
运动员4准备好了!
运动员7准备好了!
运动员9准备好了!
运动员2准备好了!
运动员10准备好了!
运动员全部都准备好了,开始比赛...
复制代码

裁判员要等待所有远动员各就各位后全部准备完毕,再开始比赛

创建同步点代码如下:

public class CountDownLatchService {

    private CountDownLatch countDownLatch = new CountDownLatch(1);

    public void runMethod(){
        try {
            System.out.println("运动员"+Thread.currentThread().getName()+"准备好了!");
            countDownLatch.await();
            System.out.println("运动员"+Thread.currentThread().getName()+"结束!");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void startMatch(){
        System.out.println("开始比赛...");
        countDownLatch.countDown();
    }

}
复制代码

创建运动员线程代码如下:

public class Runner implements Runnable {

    private CountDownLatchService service;

    public Runner(CountDownLatchService service){
        super();
        this.service=service;
    }


    @Override
    public void run() {
        service.runMethod();
    }

}
复制代码

运行类代码如下:

public class RunnerMatch {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatchService service = new CountDownLatchService();
        Runner[] runners = new Runner[10];
        for (int i = 0; i < runners.length ; i++) {
            runners[i] = new Runner(service);
            Thread thread = new Thread(runners[i]);
            thread.setName(String.valueOf(i+1));
            thread.start();
        }
        Thread.sleep(2000);
        service.startMatch();
    }
}
复制代码

运行结果如下:

运动员1准备好了!
运动员2准备好了!
运动员3准备好了!
运动员4准备好了!
运动员5准备好了!
运动员6准备好了!
运动员7准备好了!
运动员8准备好了!
运动员9准备好了!
运动员10准备好了!
开始比赛...
运动员1结束!
运动员4结束!
运动员5结束!
运动员2结束!
运动员6结束!
运动员3结束!
运动员9结束!
运动员8结束!
运动员7结束!
运动员10结束!
复制代码

猜你喜欢

转载自juejin.im/post/7086748875985256485
今日推荐