溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

多線程(七、同步計數器-CountDownLatch)

發(fā)布時間:2020-06-18 09:40:08 來源:網絡 閱讀:2117 作者:shayang88 欄目:編程語言

CountDownLatch是一個線程的同步計數器,主要有2個使用場景。

1、作為一個開關,N個子線程準備就緒,等待主線程的放行,然后子線程一起開始執(zhí)行。

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 + "執(zhí)行任務完成");
    }
}

主線程

public class Main {

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

        System.out.println("主線程啟動......");

        //創(chuàng)建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("主線程執(zhí)行任務完畢,放行......");
        countDownLatch.countDown();
        System.out.println("主線程執(zhí)行完畢");

    }
}

執(zhí)行結果

多線程(七、同步計數器-CountDownLatch)

2、主線程等待N個子線程的處理結果,子線程都完成后,主線程繼續(xù)執(zhí)行。

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 + "開始開始執(zhí)行任務......");
            Thread.sleep(2000);
            System.out.println(name + "執(zhí)行任務完成");
            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 {
        //主線程創(chuàng)建,和子線程共享
        CountDownLatch countDownLatch = new CountDownLatch(3);

        System.out.println("主線程啟動......");

        //創(chuàng)建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("主線程等待子線程執(zhí)行結果......");
        countDownLatch.await();
        System.out.println("主線程執(zhí)行完畢");

    }
}

執(zhí)行結果

多線程(七、同步計數器-CountDownLatch)

注意:CountDownLatch的計數只能使用一次,不會自動重新開始。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI