• Semaphore 信号量


    Semaphore信号量介绍

    官方的解释为

    1)Semaphore是一个计数信号量
    2)从概念上将,Semaphore包含一组许可证
    3)如果有需要的话,每次调用acquire()方法都会阻塞,直到获取一个可用的许可证
    4)每次调用release()方法都会释放持有许可证的线程,并且归还Semaphore一个可用的许可证
    5)实际上并没有真实的许可证对象供线程使用, Semaphore只是对可用的数量进行管理维护

    Semaphore内部主要通过AQS(AbstractQueuedSynchronizer)实现线程的管理 .

    一、获取单个许可

    信号量设置为3,每次获取1个许可,那么就可以连续获得3个许可。

    如下面的信号量Demo

    @Slf4j
    public class SemaphoreExample1 {
    
        private static int threadCount = 20;
    
        public static void main(String[] args) throws InterruptedException {
            ExecutorService exec = Executors.newCachedThreadPool();
            final Semaphore semaphore = new Semaphore(3);
            for(int i = 0; i < threadCount; i++){
                final int threadNum = i;
                exec.execute(()->{
                    try {
                        semaphore.acquire();//获得一个许可
                        test(threadNum);
                        semaphore.release();//释放一个许可
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                });
            }
    
            exec.shutdown();
        }
    
        private static  void test(int threadNum) throws InterruptedException {
    
            log.info("{}", threadNum);
            Thread.sleep(1000);
    
        }
    }
    

      

      打印结果如下。信号量设置为3。 差不多每隔1秒钟打印3条数据。

    二、获得多个许可

    线程数为20,信号量为3,每次获得3个许可。相当于单线程。

    @Slf4j
    public class SemaphoreExample2 {
    
        private static int threadCount = 20;
    
        public static void main(String[] args) throws InterruptedException {
            ExecutorService exec = Executors.newCachedThreadPool();
            final Semaphore semaphore = new Semaphore(3);
            for(int i = 0; i < threadCount; i++){
                final int threadNum = i;
                exec.execute(()->{
                    try {
                        semaphore.acquire(3);//获得3个许可, 信号量为3,相当于单线程执行
                        test(threadNum);
                        semaphore.release(3);//释放3个许可
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                });
            }
    
            exec.shutdown();
        }
    
        private static  void test(int threadNum) throws InterruptedException {
    
            log.info("{}", threadNum);
            Thread.sleep(1000);
    
        }
    }
    

      

    打印结果: 差不多每隔1秒打印一次结果

    21:58:55.766 [pool-1-thread-1] INFO com.bingfa.concurrency.example.aqs.SemaphoreExample2 - 0
    21:58:56.771 [pool-1-thread-2] INFO com.bingfa.concurrency.example.aqs.SemaphoreExample2 - 1
    21:58:57.771 [pool-1-thread-3] INFO com.bingfa.concurrency.example.aqs.SemaphoreExample2 - 2
    21:58:58.772 [pool-1-thread-4] INFO com.bingfa.concurrency.example.aqs.SemaphoreExample2 - 3
    21:58:59.773 [pool-1-thread-5] INFO com.bingfa.concurrency.example.aqs.SemaphoreExample2 - 4
    21:59:00.774 [pool-1-thread-6] INFO com.bingfa.concurrency.example.aqs.SemaphoreExample2 - 5
    21:59:01.774 [pool-1-thread-8] INFO com.bingfa.concurrency.example.aqs.SemaphoreExample2 - 7
    21:59:02.775 [pool-1-thread-7] INFO com.bingfa.concurrency.example.aqs.SemaphoreExample2 - 6
    21:59:03.776 [pool-1-thread-9] INFO com.bingfa.concurrency.example.aqs.SemaphoreExample2 - 8
    21:59:04.776 [pool-1-thread-10] INFO com.bingfa.concurrency.example.aqs.SemaphoreExample2 - 9
    21:59:05.777 [pool-1-thread-11] INFO com.bingfa.concurrency.example.aqs.SemaphoreExample2 - 10
    21:59:06.778 [pool-1-thread-12] INFO com.bingfa.concurrency.example.aqs.SemaphoreExample2 - 11
    21:59:07.779 [pool-1-thread-13] INFO com.bingfa.concurrency.example.aqs.SemaphoreExample2 - 12
    21:59:08.780 [pool-1-thread-14] INFO com.bingfa.concurrency.example.aqs.SemaphoreExample2 - 13
    21:59:09.780 [pool-1-thread-16] INFO com.bingfa.concurrency.example.aqs.SemaphoreExample2 - 15
    21:59:10.781 [pool-1-thread-15] INFO com.bingfa.concurrency.example.aqs.SemaphoreExample2 - 14
    21:59:11.782 [pool-1-thread-17] INFO com.bingfa.concurrency.example.aqs.SemaphoreExample2 - 16
    21:59:12.783 [pool-1-thread-18] INFO com.bingfa.concurrency.example.aqs.SemaphoreExample2 - 17
    21:59:13.784 [pool-1-thread-19] INFO com.bingfa.concurrency.example.aqs.SemaphoreExample2 - 18
    21:59:14.784 [pool-1-thread-20] INFO com.bingfa.concurrency.example.aqs.SemaphoreExample2 - 19
    

      

    三、尝试等待获取一个许可

    @Slf4j
    public class SemaphoreExample3 {
    
        private static int threadCount = 20;
    
        public static void main(String[] args) throws InterruptedException {
            ExecutorService exec = Executors.newCachedThreadPool();
            final Semaphore semaphore = new Semaphore(3);
            for(int i = 0; i < threadCount; i++){
                final int threadNum = i;
                exec.execute(()->{
                    try {
                        if(semaphore.tryAcquire(3, TimeUnit.SECONDS)) {//尝试获得一个许可
                            test(threadNum);
                            semaphore.release();//释放一个许可
                        }
    
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                });
            }
            
            exec.shutdown();
        }
    
        private static  void test(int threadNum) throws InterruptedException {
    
            log.info("{}", threadNum);
            Thread.sleep(1000);
    
        }
    }
    

      

    打印结果: 只执行了前面9个线程

  • 相关阅读:
    tomcat下的web.xml和项目中的web.xml
    tomcat日志详解
    tomcat日志及logback相关日志框架
    关于程序中使用servlet-api.jar和jsp-api.jar与服务器lib包jar包冲突的问题
    windows server数据库备份
    idea为tomcat设置虚拟地址
    GitHub
    MarkDown的用法
    SVN添加用户权限
    第一篇小记
  • 原文地址:https://www.cnblogs.com/linlf03/p/12748093.html
Copyright © 2020-2023  润新知