Java并发组件三之Semaphore

作者: TTLLong | 来源:发表于2019-07-30 20:04 被阅读7次

    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+" --- ");
        }
    }
    
    • 尝试拿许可,拿到返回true,拿不到返回false
    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);
        }
    }
    
    • 给定时间内拿到许可返回true,拿不到返回false
    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);
        }
    }
    

    相关文章

      网友评论

        本文标题:Java并发组件三之Semaphore

        本文链接:https://www.haomeiwen.com/subject/aisorctx.html