1. 程式人生 > >CountDownLatch、CyclicBarrier及Semaphore的用法示例

CountDownLatch、CyclicBarrier及Semaphore的用法示例

一、參考blog

https://www.cnblogs.com/dolphin0520/p/3920397.html

二、CountDownLatch

個人把它類比於一個持有計數的閘門,每到達這個閘門一個執行緒,計數減1,當計數為0時再執行閘門後續的動作。同時閘門失效了(只能用一次)。

public static void main(String[] args) {
        Executor fix = Executors.newFixedThreadPool(5);
        CountDownLatch latch = new CountDownLatch(2);
        Runnable runner 
= () -> { System.out.println("[" + Thread.currentThread().getName() + "]" + " started"); System.out.println("[" + Thread.currentThread().getName() + "]" + " finished"); latch.countDown(); }; fix.execute(runner); fix.execute(runner);
try { System.out.println("[" + Thread.currentThread().getName() + "]" + " waiting on latch..."); latch.await(); System.out.println("[" + Thread.currentThread().getName() + "]" + " ok, i can do my work"); } catch (InterruptedException e) { e.printStackTrace(); } }

 三、CyclicBarrier

用法差不多,特點:可以重複使用。

    public static void main(String[] args) {
        Executor fix = Executors.newFixedThreadPool(5);
        CyclicBarrier barrier = new CyclicBarrier(2);
        Runnable runner = () -> {
            try {
                Thread.sleep(new Random().nextInt(10000));
                System.out.println("[" + Thread.currentThread().getName() + "]" + " wait on barri " + new Date());
                barrier.await();
                System.out.println("[" + Thread.currentThread().getName() + "]" + " condition ok, do my work " + System.currentTimeMillis());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
        };
        fix.execute(runner);
        fix.execute(runner);
    }

四、Semaphore

 向量,有點像鎖。

import java.util.Date;
import java.util.Random;
import java.util.concurrent.*;

public class Test {
    //    
    public static void main(String[] args) {
        Executor fix = Executors.newFixedThreadPool(5);
        Semaphore semaphore = new Semaphore(2);

        Runnable runner = () -> {
            try {
                semaphore.acquire();
                System.out.println("[" + Thread.currentThread().getName() + "]" + " get a semaphore " + new Date());
                Thread.sleep(2000);
                System.out.println("[" + Thread.currentThread().getName() + "]" + " release a semaphore " + new Date());
                semaphore.release();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };

        for (int i = 0; i < 5; i++) {
            fix.execute(runner);
        }

    }