码迷,mamicode.com
首页 > 编程语言 > 详细

多线程(七、同步计数器-CountDownLatch)

时间:2019-06-03 20:13:12      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:线程等待   print   创建   exce   结果   runnable   out   system   技术   

CountDownLatch是一个线程的同步计数器,主要有2个使用场景。

1、作为一个开关,N个子线程准备就绪,等待主线程的放行,然后子线程一起开始执行。

Task

package com.jane;

import java.util.concurrent.CountDownLatch;

public class Task implements Runnable {
    private CountDownLatch countDownLatch;
    private String name;

    public Task(CountDownLatch countDownLatch, String name) {
        this.countDownLatch = countDownLatch;
        this.name = name;
    }

    @Override
    public void run() {
        try {
            System.out.println(name + "开始等待开关......");
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(name + "执行任务完成");
    }
}

主线程

public class Main {

    public static void main(String[] args) throws InterruptedException {
        //主线程创建,和子线程共享
        CountDownLatch countDownLatch = new CountDownLatch(1);

        System.out.println("主线程启动......");

        //创建3个子线程
        Thread t1 = new Thread(new Task(countDownLatch, "线程-1"));
        Thread t2 = new Thread(new Task(countDownLatch, "线程-2"));
        Thread t3 = new Thread(new Task(countDownLatch, "线程-3"));

        t1.start();
        t2.start();
        t3.start();

        Thread.sleep(2000);
        System.out.println("主线程执行任务完毕,放行......");
        countDownLatch.countDown();
        System.out.println("主线程执行完毕");

    }
}

执行结果

技术图片

2、主线程等待N个子线程的处理结果,子线程都完成后,主线程继续执行。

Task

package com.jane;

import java.util.concurrent.CountDownLatch;

public class Task1 implements Runnable {
    private CountDownLatch countDownLatch;
    private String name;

    public Task1(CountDownLatch countDownLatch, String name) {
        this.countDownLatch = countDownLatch;
        this.name = name;
    }

    @Override
    public void run() {
        try {
            System.out.println(name + "开始开始执行任务......");
            Thread.sleep(2000);
            System.out.println(name + "执行任务完成");
            countDownLatch.countDown();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

主线程

package com.jane;

import java.util.concurrent.CountDownLatch;

public class Main1 {

    public static void main(String[] args) throws InterruptedException {
        //主线程创建,和子线程共享
        CountDownLatch countDownLatch = new CountDownLatch(3);

        System.out.println("主线程启动......");

        //创建3个子线程
        Thread t1 = new Thread(new Task1(countDownLatch, "线程-1"));
        Thread t2 = new Thread(new Task1(countDownLatch, "线程-2"));
        Thread t3 = new Thread(new Task1(countDownLatch, "线程-3"));

        t1.start();
        t2.start();
        t3.start();

        System.out.println("主线程等待子线程执行结果......");
        countDownLatch.await();
        System.out.println("主线程执行完毕");

    }
}

执行结果

技术图片

注意:CountDownLatch的计数只能使用一次,不会自动重新开始。

多线程(七、同步计数器-CountDownLatch)

标签:线程等待   print   创建   exce   结果   runnable   out   system   技术   

原文地址:https://blog.51cto.com/janephp/2404460

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!