@@ -96,31 +96,52 @@ Semaphore 类似于操作系统中的信号量,可以控制对互斥资源的
9696
9797``` java
9898public class SemaphoreExample {
99-
10099 public static void main (String [] args ) {
101- final int clientCount = 3 ;
102- final int totalRequestCount = 10 ;
103- Semaphore semaphore = new Semaphore (clientCount);
104- ExecutorService executorService = Executors . newCachedThreadPool();
105- for (int i = 0 ; i < totalRequestCount; i++ ) {
106- executorService. execute(()- > {
107- try {
108- semaphore. acquire();
109- System . out. print(semaphore. availablePermits() + " " );
110- } catch (InterruptedException e) {
111- e. printStackTrace();
112- } finally {
113- semaphore. release();
100+ final int threadNum= 1 ;
101+ final int totalThread= 10 ;
102+
103+ Semaphore semaphore= new Semaphore (threadNum);
104+
105+ ExecutorService service= Executors . newCachedThreadPool();
106+
107+ for (int i= 0 ;i< totalThread;i++ ){
108+ final int num= i;
109+ service. execute(new Runnable () {
110+ @Override
111+ public void run () {
112+ try {
113+ semaphore. acquire();
114+ test(num);
115+ } catch (InterruptedException e) {
116+ e. printStackTrace();
117+ }finally {
118+ semaphore. release();
119+ }
114120 }
115121 });
116122 }
117- executorService. shutdown();
123+ service. shutdown();
124+ }
125+
126+ private static void test (int i ) throws InterruptedException {
127+ System . out. println(" Thread: " + i);
128+ Thread . sleep(1000 );
118129 }
119130}
120131```
121132
122133``` html
123- 2 1 2 2 2 2 2 1 2 2
134+ // 每隔一秒钟出现一次
135+ Thread: 0
136+ Thread: 1
137+ Thread: 2
138+ Thread: 3
139+ Thread: 4
140+ Thread: 5
141+ Thread: 7
142+ Thread: 6
143+ Thread: 8
144+ Thread: 9
124145```
125146
126147### 4. CountDownLatch 和 CyclicBarrier 比较
@@ -217,51 +238,67 @@ java.util.concurrent.BlockingQueue 接口有以下阻塞队列的实现:
217238
218239``` java
219240public class ProducerConsumer {
241+ private static BlockingQueue<String > queue= new LinkedBlockingQueue<> ();
220242
221- private static BlockingQueue<String > queue = new ArrayBlockingQueue<> (5 );
222-
223- private static class Producer extends Thread {
243+ private static class Producer extends Thread {
224244 @Override
225245 public void run () {
226246 try {
227247 queue. put(" product" );
248+ System . out. println(" produce..." );
228249 } catch (InterruptedException e) {
229250 e. printStackTrace();
230251 }
231- System . out. print(" produce.." );
232252 }
233253 }
234254
235- private static class Consumer extends Thread {
236-
255+ private static class Consumer extends Thread {
237256 @Override
238257 public void run () {
239258 try {
240- String product = queue. take();
259+ queue. take();
260+ System . out. println(" consume..." );
241261 } catch (InterruptedException e) {
242262 e. printStackTrace();
243263 }
244- System . out. print(" consume.." );
245264 }
246265 }
247- }Copy to clipboardErrorCopied
248- public static void main(String [] args) {
249- for (int i = 0 ; i < 2 ; i++ ) {
250- Producer producer = new Producer ();
251- producer. start();
252- }
253- for (int i = 0 ; i < 5 ; i++ ) {
254- Consumer consumer = new Consumer ();
255- consumer. start();
256- }
257- for (int i = 0 ; i < 3 ; i++ ) {
258- Producer producer = new Producer ();
259- producer. start();
266+
267+ public static void main (String [] args ) {
268+ for (int i= 0 ;i< 2 ;i++ ){
269+ Producer p= new Producer ();
270+ p. start();
271+ }
272+
273+ for (int i= 0 ;i< 5 ;i++ ){
274+ Consumer c= new Consumer ();
275+ c. start();
276+ }
277+
278+ for (int i= 0 ;i< 3 ;i++ ){
279+ Producer p= new Producer ();
280+ p. start();
281+ }
282+
260283 }
261- }Copy to clipboardErrorCopied
262- produce.. produce.. consume.. consume.. produce.. consume.. produce.. consume.. produce.. consume.. Copy to clipboardErrorCopied
284+ }
285+ ```
286+
287+ ``` html
288+ produce...
289+ produce...
290+ consume...
291+ consume...
292+ produce...
293+ produce...
294+ consume...
295+ consume...
296+ produce...
297+ consume...
263298```
264299
300+
301+
265302### 3. ForkJoin
266303
267304主要用于并行计算中,和 MapReduce 原理类似,都是把大的计算任务拆分成多个小任务并行计算。
0 commit comments