Semaphore

Posted sakura1027

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Semaphore相关的知识,希望对你有一定的参考价值。

 1 package com.mmall.concurrency.demo.aqs;
 2 
 3 import java.util.concurrent.ExecutorService;
 4 import java.util.concurrent.Executors;
 5 import java.util.concurrent.Semaphore;
 6 
 7 public class SemaphoreDemo1 {
 8     private final static int threadCount = 20;
 9 
10     public static void main(String[] args) throws Exception {
11         ExecutorService exec = Executors.newCachedThreadPool();
12         final Semaphore semaphore = new Semaphore(3);
13         for (int i = 0; i < threadCount; i++) {
14             final int threadNum = i;
15             exec.execute(() -> {
16                 try {
17                     semaphore.acquire(); //获取一个许可
18                     fun(threadNum);
19                     semaphore.release(); //释放一个许可
20                 } catch (Exception e) {
21                     e.printStackTrace();
22                 }
23             });
24         }
25         exec.shutdown();
26     }
27 
28     private static void fun(int threadNum) throws Exception {
29         System.out.println(threadNum);
30         Thread.sleep(1000);
31     }
32 }
 1 package com.mmall.concurrency.demo.aqs;
 2 
 3 import java.util.concurrent.ExecutorService;
 4 import java.util.concurrent.Executors;
 5 import java.util.concurrent.Semaphore;
 6 
 7 public class SemaphoreDemo2 {
 8     private final static int threadCount = 20;
 9 
10     public static void main(String[] args) throws Exception {
11         ExecutorService exec = Executors.newCachedThreadPool();
12         final Semaphore semaphore = new Semaphore(3);
13         for (int i = 0; i < threadCount; i++) {
14             final int threadNum = i;
15             exec.execute(() -> {
16                 try {
17                     semaphore.acquire(3); //获取多个许可
18                     fun(threadNum);
19                     semaphore.release(3); //释放多个许可
20                 } catch (Exception e) {
21                     e.printStackTrace();
22                 }
23             });
24         }
25         exec.shutdown();
26     }
27 
28     private static void fun(int threadNum) throws Exception {
29         System.out.println(threadNum);
30         Thread.sleep(1000);
31     }
32 }
 1 package com.mmall.concurrency.demo.aqs;
 2 
 3 import java.util.concurrent.ExecutorService;
 4 import java.util.concurrent.Executors;
 5 import java.util.concurrent.Semaphore;
 6 
 7 public class SemaphoreDemo3 {
 8     private final static int threadCount = 20;
 9 
10     public static void main(String[] args) throws Exception {
11         ExecutorService exec = Executors.newCachedThreadPool();
12         final Semaphore semaphore = new Semaphore(3);
13         for (int i = 0; i < threadCount; i++) {
14             final int threadNum = i;
15             exec.execute(() -> {
16                 try {
17                     if (semaphore.tryAcquire()) { //尝试获取一个许可
18                         test(threadNum);
19                         semaphore.release(); //释放一个许可
20                     }
21                 } catch (Exception e) {
22                     e.printStackTrace();
23                 }
24             });
25         }
26         exec.shutdown();
27     }
28 
29     private static void test(int threadNum) throws Exception {
30         System.out.println(threadNum);
31         Thread.sleep(1000);
32     }
33 }
 1 package com.mmall.concurrency.demo.aqs;
 2 
 3 import java.util.concurrent.ExecutorService;
 4 import java.util.concurrent.Executors;
 5 import java.util.concurrent.Semaphore;
 6 import java.util.concurrent.TimeUnit;
 7 
 8 public class SemaphoreDemo4 {
 9     private final static int threadCount = 20;
10 
11     public static void main(String[] args) throws Exception {
12         ExecutorService exec = Executors.newCachedThreadPool();
13         final Semaphore semaphore = new Semaphore(3);
14         for (int i = 0; i < threadCount; i++) {
15             final int threadNum = i;
16             exec.execute(() -> {
17                 try {
18                     if (semaphore.tryAcquire(5000, TimeUnit.MILLISECONDS)) { //尝试获取一个许可
19                         fun(threadNum);
20                         semaphore.release(); //释放一个许可
21                     }
22                 } catch (Exception e) {
23                     e.printStackTrace();
24                 }
25             });
26         }
27         exec.shutdown();
28     }
29 
30     private static void fun(int threadNum) throws Exception {
31         System.out.println(threadNum);
32         Thread.sleep(1000);
33     }
34 }

 

以上是关于Semaphore的主要内容,如果未能解决你的问题,请参考以下文章

Java并发多线程编程——Semaphore

Java Semaphore实现高并发场景下的流量控制(附源码) | 实用代码架构

Semaphore回顾

互斥锁 & 共享锁

用synchronized实现Semaphore

用synchronized实现Semaphore