亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

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

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

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

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

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("主線程執行完畢");

    }
}

執行結果

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

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的計數只能使用一次,不會自動重新開始。

向AI問一下細節

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

AI

松滋市| 邵武市| 桦川县| 商水县| 礼泉县| 昌平区| 金寨县| 新民市| 双鸭山市| 邹平县| 光山县| 修武县| 龙泉市| 左云县| 蒲城县| 抚远县| 巴中市| 孝义市| 大理市| 锡林郭勒盟| 信宜市| 满洲里市| 沾化县| 墨竹工卡县| 仪征市| 巧家县| 孟连| 双辽市| 依兰县| 九江市| 广水市| 淮滨县| 连江县| 石狮市| 苏州市| 朝阳县| 山丹县| 嘉黎县| 紫金县| 南昌市| 道真|