中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

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

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

多線程(七、同步計數器-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

林州市| 洪湖市| 乐清市| 皋兰县| 连州市| 措美县| 南丰县| 石家庄市| 柯坪县| 涟源市| 壶关县| 水城县| 遂昌县| 封开县| 囊谦县| 寿光市| 宕昌县| 大城县| 唐海县| 金门县| 孟州市| 肇庆市| 自贡市| 昌宁县| 南投市| 子洲县| 铁力市| 葫芦岛市| 西充县| 萨迦县| 科技| 绵阳市| 东乡县| 双江| 台东市| 九寨沟县| 金湖县| 勐海县| 和顺县| 金门县| 龙川县|