Semaphore semaphore

1. Obtain a single license

The semaphore is set to 3, and one license is obtained each time, so you can obtain three licenses in succession.

As the following semaphore 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);

    }
}

  

  The print result is as follows. The semaphore is set to 3. 3 data are printed almost every 1 second.

 

2. Obtain multiple licenses

The number of threads is 20, the semaphore is 3, and 3 licenses are obtained each time. Equivalent to single thread.

@ 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); // Obtain 3 licenses, the semaphore is 3, equivalent to single thread execution 
                    test (threadNum); 
                    semaphore.release (3); // Release 3 licenses 
                } catch (InterruptedException e) { 
                    e.printStackTrace (); 
                } 
            });
        }

        exec.shutdown();
    }

    private static  void test(int threadNum) throws InterruptedException {

        log.info("{}", threadNum);
        Thread.sleep(1000);

    }
}

  

Print results: print results almost every 1 second

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

  

 

3. Try to wait for a license

@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);

    }
}

  

Print result: Only the first 9 threads are executed

 

Guess you like

Origin www.cnblogs.com/linlf03/p/12748093.html