work hard work smart

专注于Java后端开发。 不断总结,举一反三。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Semaphore 信号量

Posted on 2020-04-21 21:44  work hard work smart  阅读(132)  评论(0编辑  收藏  举报

Semaphore: 信号量,用于控制访问某一公共资源的并发线程数

 

Semaphore信号量介绍

官方的解释为

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

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

 

应用场景
我们有大量的线程在完成一个巨量任务的时候,但是某一公共资源却有限定了线程的链接树,这时候就需要对这些大量线程访问这一公共资源做控制。例如当我们有上百个线程需要处理本地上G的数据文件,每个线程处理完成之后需要把结果写到数据库,而数据库只支持十个线程的并发链接,此时,对数据库的链接我们就可以通过Semaphore来控制最大连接数。

 

一、获取单个许可

信号量设置为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个线程