码迷,mamicode.com
首页 > 其他好文 > 详细

Semaphore

时间:2018-04-11 21:52:15      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:final   shu   pack   count   tst   thread   获取   service   ack   

 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

标签:final   shu   pack   count   tst   thread   获取   service   ack   

原文地址:https://www.cnblogs.com/sakura1027/p/8798405.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!