Android开发Android技术知识Android开发

Java并发组件三之Semaphore

2019-07-30  本文已影响7人  TTLLong

Semaphore

相关文章:
  1. CountDownLatch
  2. CyclicBarriar--循环屏障
  3. Semaphore--信号量
使用场景:常用于使用有限的资源,限制线程并发的最大数量。
  1. 设定信号量的最大个数:Semaphore semaphore=new Semaphore(3);
  2. 获取信号量:
    • semaphore.acquire(); //获取信号量
    • semaphore.acquire(3); //获取多个许可
    • semaphore.tryAcquire(3); //尝试获取多个许可
    • semaphore.tryAcquire(5, TimeUnit.SECONDS); //给定时间内获取许可
  3. 释放信号量:semaphore.release();
公平性:
默认情况下,信号量是非公平性的(先等待先执行为公平。类似于买东西的时候大家排队付款,先来的先付款是公平的。但是这时候有人插队,那就是非公平的)
image.png image.png
示例代码:
class SemaphoreExample1 {
    private static final int total=20;

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        Semaphore semaphore=new Semaphore(3);
        for (int i=0;i<total;i++){
            final int threadNum=i;
            executorService.execute(()-> {
                try {
                    semaphore.acquire();//获取许可
                    test(threadNum);
                    semaphore.release();//释放许可
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }

        executorService.shutdown();
    }

    private static void test(int time){
        System.out.println(time+" --- ");
    }
class SemaphoreExample2 {
    private static final int total=20;

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        Semaphore semaphore=new Semaphore(3);
        for (int i=0;i<total;i++){
            final int threadNum=i;
            executorService.execute(()-> {
                try {
                    semaphore.acquire(3);//获取多个许可
                    test(threadNum);
                    semaphore.release(3);//释放多个许可
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }

        executorService.shutdown();
    }

    private static void test(int time){
        System.out.println(time+" --- ");
    }
}
class SemaphoreExample3 {
    private static final int total=20;

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        Semaphore semaphore=new Semaphore(5);
        for (int i=0;i<total;i++){
            final int threadNum=i;
            executorService.execute(()-> {
                try {
                    //尝试同时拿多个许可。拿到返回true,拿不到返回false
                    //第一次拿3个,第2次只拿到了2个,不满足3个,
                    if (semaphore.tryAcquire(3)){
                        test(threadNum);
                        semaphore.release(3);//释放多个许可
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }

        executorService.shutdown();
    }

    private static void test(int time) throws InterruptedException {
        System.out.println(time+" --- ");
        Thread.sleep(1000);
    }
}
class SemaphoreExample4 {
    private static final int total=20;

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        Semaphore semaphore=new Semaphore(3);
        for (int i=0;i<total;i++){
            final int threadNum=i;
            executorService.execute(()-> {
                try {
                    if (semaphore.tryAcquire(5, TimeUnit.SECONDS)){//尝试拿许可,时间超过5秒,返回false
                        test(threadNum);
                        semaphore.release();//释放多个许可
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }

        executorService.shutdown();
    }

    private static void test(int time) throws InterruptedException {
        System.out.println(time+" --- ");
        Thread.sleep(1000);
    }
}
上一篇下一篇

猜你喜欢

热点阅读