91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

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

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

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

华阴市| 永寿县| 长顺县| 渝北区| 临夏县| 天等县| 南召县| 项城市| 长汀县| 揭东县| 长乐市| 莱西市| 沾化县| 朝阳市| 思南县| 黑水县| 桂东县| 永济市| 绵阳市| 临沂市| 荣成市| 四子王旗| 潍坊市| 沛县| 延庆县| 思南县| 巨野县| 乐都县| 嵊泗县| 武功县| 邹城市| 开远市| 双峰县| 诸暨市| 兴宁市| 新巴尔虎左旗| 南皮县| 高青县| 霍山县| 汶上县| 郁南县|