Comments (1)
发现这已经是一个2019年的Issue了,想必提问者随着阅历的增加早已找到了答案。而我作为一个正在学习的萌新在2021年看到了这个问题,便尝试着来解答一下。希望对未来的萌新有所帮助
- 我写了一段至少打印顺序正确的demo
public void testCountDownLatch() throws InterruptedException {
CountDownLatch judgeLatch = new CountDownLatch(1);
CountDownLatch sportLatch = new CountDownLatch(8);
CountDownLatch sportWaitLatch = new CountDownLatch(8);
ExecutorService executorService = Executors.newFixedThreadPool(8);
for (int i = 0; i < 8; i++) {
executorService.execute(() -> {
try {
log.info(Thread.currentThread().getName() + "运动员等待裁判员响哨!!!");
sportWaitLatch.countDown();
judgeLatch.await();
log.info(Thread.currentThread().getName() + "正在全力冲刺!!!");
log.info(Thread.currentThread().getName() + "到达终点");
sportLatch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
sportWaitLatch.await();
log.info("裁判员发号施令啦!!!");
judgeLatch.countDown();
sportLatch.await();
log.info("所有运动员到达终点,比赛结束");
}
- 可以看出区别在于打印“裁判员发号施令啦!!!”语句时,我用一个新的CountDownLatch对象await住了其他线程。
- 那么很容易就知道原本代码的问题在于:主线程在执行打印“裁判员发号施令啦!!!”时,线程池的遍历执行还没有集中执行到await()处。
from java-concurrency.
Related Issues (19)
- 关于《彻底理解volatile》中示例代码的问题 HOT 4
- 07.三大性质总结:原子性、可见性以及有序性 HOT 1
- 02 节里的图片不显示 HOT 2
- 文中源碼是java 哪個版本?
- Exchanger 代碼有問題
- 建议创建一个gitbook方便阅读 HOT 2
- 关于AbstractQueuedSynchronizer类中enq方法并没有让tail指向新的尾节点的两个疑问 HOT 1
- 内存使用计算问题
- 并发编程的优缺点中几个错别字
- 线程状态转换的错误,WAITING不能直接转变为RUNNABLE
- 彻底理解synchronized章节 3.2 Java对象头 里的图片未显示
- 感觉原子类的分类不太合理,有点乱,如下分类感觉更好 HOT 1
- 最后的例子 HOT 1
- 关于偏向锁升级为轻量级锁的疑问
- 图挂了
- 图片不显示格式问题 HOT 1
- 4.彻底理解synchronized HOT 3
- 3.java内存模型以及happens-before规则
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from java-concurrency.