互聯網面試題之CountDownLatch/CyclicBarrier/Semaphore用過嗎

CountDownLatch

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * 秦滅六國,一統天下
 */
public class CountDownLatchDemo {

    public static final int NUM = 6;

    public static void main(String[] args) {
        // 設置計數器值
        CountDownLatch latch = new CountDownLatch(NUM);

        for(int i = 1; i <= NUM; i++) {
            new Thread(()-> {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                System.out.println( Thread.currentThread().getName() +  "員工下班");

                // 計數器值減1
                latch.countDown();

            }, String.valueOf(i)).start();
        }

        try {
            // 調用await方法,調用線程會被阻塞,其他線程調用countDown方法會將計數器減1
            // 當計數器的值變爲0時,線程會被喚醒,繼續執行。
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("管理員鎖門");
    }

}


CyclicBarrier

import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;

/**
 * 集齊七龍珠,召喚神龍
 * 人到齊了纔開會
 *
 * CyclicBarrier 的字面意思是可循環(Cyclic)使用的屏障(Barrier)
 * 可以讓一個線程到達一個屏障)(同步點)時被阻塞,直到最後一個線程到達屏障時,屏障纔會開門。
 * 所有被屏障攔截的線程纔會繼續幹活,線程進入屏障通過CyclicBarrier的await方法。
 */
public class CyclicBarrierDemo {

    public static void main(String[] args) {
        CyclicBarrier barrier = new CyclicBarrier(7, ()-> {
            System.out.println(Thread.currentThread().getName() + "召喚神龍");
        });

        for(int i = 1; i <= 7; i++) {
            final int tmp = i;
            new Thread(()-> {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "收集到第" + tmp + "顆龍珠");

                try {
                    // 等待
                    barrier.await();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }, String.valueOf(i)).start();
        }
    }
}

Semaphore

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

/**
 * 信號量主要用於兩個目的,一個是用於多個共享資源的互斥使用,另一個用於併發線程數控制。
 * 爭車位
 */
public class SemaphoreDemo {

    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(3); // 模擬3個停車位

        for(int i = 0; i < 6; i++) {
            new Thread(()-> {
                try {
                    semaphore.acquire();
                    System.out.println(Thread.currentThread().getName() + "搶到車位");
                    TimeUnit.SECONDS.sleep(3);

                    System.out.println(Thread.currentThread().getName() + "停3秒後離開");

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    semaphore.release();
                }
            }, String.valueOf(i)).start();
        }

    }

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章