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个线程

猜你喜欢

转载自www.cnblogs.com/linlf03/p/12748093.html