Skip to content

Commit dbef653

Browse files
author
mj
committed
CyclickBarrier 栅栏的案例
1 parent 86a121c commit dbef653

1 file changed

Lines changed: 65 additions & 5 deletions

File tree

app/src/main/java/com/mj/android_note/thread/thread_pool/ThreadPoolDemo.java

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.mj.android_note.thread.thread_pool;
22

3+
import java.util.concurrent.BrokenBarrierException;
34
import java.util.concurrent.ConcurrentHashMap;
5+
import java.util.concurrent.CyclicBarrier;
46
import java.util.concurrent.ExecutorService;
57
import java.util.concurrent.Executors;
68
import java.util.concurrent.LinkedBlockingQueue;
@@ -9,6 +11,7 @@
911
import java.util.concurrent.ThreadPoolExecutor;
1012
import java.util.concurrent.TimeUnit;
1113
import java.util.concurrent.atomic.AtomicInteger;
14+
import java.util.concurrent.atomic.AtomicReference;
1215
import java.util.concurrent.locks.ReentrantLock;
1316

1417
public class ThreadPoolDemo {
@@ -118,6 +121,15 @@ public static void main(String[] args) {
118121

119122
// java线程工具包 semaphore countdownLatch cyclicBarrier
120123
new JavaThreadTools();
124+
125+
// 验证i++ 与 ++ i的区别
126+
checkIPP();
127+
}
128+
129+
private static void checkIPP() {
130+
int i = 0;
131+
printLog("i ++ : " + (++i));
132+
// 打印++i:结果为1, 如果打印i++则打印结果为 0
121133
}
122134

123135

@@ -469,17 +481,18 @@ void set(T t) {
469481
private static class JavaThreadTools {
470482

471483
JavaThreadTools() {
472-
eatBreakfast();
484+
// eatBreakfast();
485+
placeTheOrder();
473486
}
474487

475-
// 10个人要吃早餐
476-
private final int COUNT = 10;
477488
// 但是只有三个凳子
478489
Semaphore semaphore = new Semaphore(3);
479490

480-
// 吃早餐
491+
// 吃早餐,用信号量来实现
481492
void eatBreakfast() {
482493
ExecutorService executorService = Executors.newCachedThreadPool();
494+
// 10个人要吃早餐
495+
int COUNT = 10;
483496
for (int i = 0; i < COUNT; i++) {
484497
executorService.execute(() -> {
485498
try {
@@ -489,14 +502,61 @@ void eatBreakfast() {
489502
Thread.sleep(1000);
490503
} catch (InterruptedException e) {
491504
e.printStackTrace();
492-
}finally {
505+
} finally {
493506
printLog(Thread.currentThread().getName() + "---吃完了...");
494507
semaphore.release();
495508
}
496509
});
497510
}
498511
}
499512

513+
// 栅栏
514+
CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
515+
516+
// 下单过程,通过 CyclicBarrier(栅栏来完成)
517+
void placeTheOrder() {
518+
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);
519+
AtomicReference<String> userToken = new AtomicReference<>();
520+
AtomicReference<String> commodityPrice = new AtomicReference<>();
521+
scheduledExecutorService.execute(() -> {
522+
try {
523+
524+
userToken.set("i am userToken");
525+
// 获取用户token
526+
cyclicBarrier.await();
527+
} catch (BrokenBarrierException e) {
528+
e.printStackTrace();
529+
} catch (InterruptedException e) {
530+
e.printStackTrace();
531+
}
532+
});
533+
scheduledExecutorService.execute(() -> {
534+
try {
535+
536+
Thread.sleep(1000);
537+
commodityPrice.set("1.25元");
538+
// 拿到订单token
539+
cyclicBarrier.await();
540+
} catch (BrokenBarrierException e) {
541+
e.printStackTrace();
542+
} catch (InterruptedException e) {
543+
e.printStackTrace();
544+
}
545+
});
546+
try {
547+
cyclicBarrier.await();
548+
printLog("拿到用户token:" + userToken.get() + "--拿到商品价格:" + commodityPrice.get() + "--开始下单...");
549+
if (!scheduledExecutorService.isShutdown()) {
550+
scheduledExecutorService.shutdown();
551+
}
552+
} catch (BrokenBarrierException e) {
553+
e.printStackTrace();
554+
} catch (InterruptedException e) {
555+
e.printStackTrace();
556+
}
557+
558+
}
559+
500560
}
501561

502562

0 commit comments

Comments
 (0)