使用CountDownLatch和CyclicBarrier处理并发线程
{
IValueCallback remoteCallback = new IValueCallback.Stub() {
<strong><span style="color:#ff0000;">(B)</span></strong> public void onReceiveValue(final Bundle value) throws RemoteException {
synchronized (syncObject) {
mReturnValue = arg.result;
syncObject.notify(); //运行完成,notify通知代码继续进行
}
}
};
boolean bSuccess = false;
synchronized (syncObject) {
<strong><span style="color:#ff0000;">(A) </span></strong>sendRequest(CommandConstant.COMMAND_NAVIGATION_ITEM_EXIST, arg, remoteCallback);
try {
syncObject.wait(5000); //等待Callback部分运行完成
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return mReturnValue;
}
CyclicBarrier 出场了。
(从未听说过Java这两个类的请举手!我先举为敬……)
A is initialized with a given count. The
CountDownLatch
await methods block until the current count reaches zero due to invocations of the
countDown() method, after which all waiting threads are released and any subsequent invocations of
await return immediately. This is a one-shot phenomenon -- the count cannot be reset.
countDown()方法)。
你能够向CountDownLatch对象设置一个初始的数字作为计数值。不论什么调用这个对象上的await()方法都会堵塞,直到这个计数器的计数值被其它的线程减为0为止。
上面的代码改动为:
{
CountDownLatch cdl = new CountDownLatch (2);//2次的计数器
IValueCallback remoteCallback = new IValueCallback.Stub() {
(B) public void onReceiveValueA(final Bundle value) throws RemoteException {
mReturnValue = arg.result && mReturnValue ;
cdl.countDown(); // 降低一次计数
}
public void onReceiveValueB(final Bundle value) throws RemoteException {
mReturnValue = arg.result&& mReturnValue ;
cdl.countDown(); // 降低一次计数
}
}
};
boolean bSuccess = false;
(A) sendRequest(CommandConstant.COMMAND_NAVIGATION_ITEM_EXIST, arg, remoteCallback);
try {
cdl.await() //等计数器清零后返回结果
} catch (InterruptedException e) {
e.printStackTrace();
}
return mReturnValue;
}
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier; public class LOLGame {
public static void main(String[] args) {
CyclicBarrier cyclicBarrier = new CyclicBarrier(10, new Runnable() { @Override
public void run() {
System.out.println("欢迎来到召唤师峡谷!");
}
}); for (int i = 0; i < 10; i++) {
new Thread(new Player(i, cyclicBarrier)).start();
}
} } class Player implements Runnable { private CyclicBarrier cyclicBarrier;
private int id; public Player(int id, CyclicBarrier cyclicBarrier) {
this.cyclicBarrier = cyclicBarrier;
this.id = id;
} @Override
public void run() {
try {
System.out.println("玩家" + id + "正在读进度条...");
cyclicBarrier.await();
System.out.println("玩家" + id + "购买装备...");
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}
}
的Demo就不再写了,大家能够举一反三,上网搜一下资料。有非常多问题的解都非常精妙。
与晦涩的同步锁相比,计数器的实现会更easy将多线程问题抽象,将精力投入到详细逻辑的严谨性,而非投入精力为可能的死锁和性能消耗而头疼。
版权声明:本文博客原创文章。博客,未经同意,不得转载。
使用CountDownLatch和CyclicBarrier处理并发线程的更多相关文章
- Java并发编程之CountDownLatch,CyclicBarrier实现一组线程相互等待、唤醒
java多线程应用场景不少,有时自己编写代码又不太容易实现,好在concurrent包提供了不少实现类,还有google的guava包更是提供了一些最佳实践,这让我们在面对一些多线程的场景时,有了不少 ...
- CountDownLatch 和 CyclicBarrier 的基本使用
CountDownLatch 和 CyclicBarrier 是并发编程中常用的辅助类,两者使用上有点类似,但又有不同. 一.CountDownLatch CountDownLatch 可是实现类似计 ...
- Java并发编程:CountDownLatch、CyclicBarrier和Semaphore
Java并发编程:CountDownLatch.CyclicBarrier和Semaphore 在java 1.5中,提供了一些非常有用的辅助类来帮助我们进行并发编程,比如CountDownLatch ...
- 并发工具类:CountDownLatch、CyclicBarrier、Semaphore
在多线程的场景下,有些并发流程需要人为来控制,在JDK的并发包里提供了几个并发工具类:CountDownLatch.CyclicBarrier.Semaphore. 一.CountDownLatch ...
- 使用Java辅助类(CountDownLatch、CyclicBarrier、Semaphore)并发编程
在java 1.5中,提供了一些非常有用的辅助类来帮助我们进行并发编程,比如CountDownLatch,CyclicBarrier和Semaphore,今天我们就来学习一下这三个辅助类的用法 一.C ...
- 并发编程(七)——AbstractQueuedSynchronizer 之 CountDownLatch、CyclicBarrier、Semaphore 源码分析
这篇,我们的关注点是 AQS 最后的部分,共享模式的使用.本文先用 CountDownLatch 将共享模式说清楚,然后顺着把其他 AQS 相关的类 CyclicBarrier.Semaphore 的 ...
- 并发编程(二)—— CountDownLatch、CyclicBarrier和Semaphore
本文将讲解CountDownLatch,CyclicBarrier和Semaphore这三个并发包里面的辅助类. CountDownLatch 正如每个Java文档所描述的那样,CountDownLa ...
- Java中的并发工具类(CountDownLatch、CyclicBarrier、Semaphore、Exchanger)
在JDK的并发包里提供了很多有意思的并发工具类.CountDownLatch.CyclicBarrier和Semaphore 工具类提供了一种并发流程控制的手段,Exchanger 工具类则提供了在线 ...
- CountDownLatch和CyclicBarrier模拟同时并发请求
有时候要测试一下某个功能的并发能力,又不要想借助于其他测试工具,索性就自己写简单的demo模拟一个并发请求就最方便了.如果熟悉jemter的测试某接口的并发能力其实更专业,此处只是自己折腾着玩. Co ...
随机推荐
- POJ 3979 分数减法【数学问题的探讨】
将a/b和c/d简化一下就可以了 分数加减法 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12588 Accepted ...
- Android开发之异步具体解释(二)之AsyncTask
请尊重他人的劳动成果,转载请注明出处:Android开发之异步具体解释(二)之AsyncTask http://blog.csdn.net/fengyuzhengfan/article/details ...
- Angular绑定数据时转义html标签
AngularJs在绑定数据时默认会以文本的形式出现在页面上,比如我现在有这样一段代码 <div ng-controller="testCtrl">{{data}}&l ...
- python 导入库问题
最终解决如下面:我不知道有没有多余的空间 from django.conf import settings from sys import path path.extend(['/home/zoues ...
- poj 3400 Dropping the stones
//next_permutation全阵列 # include <stdio.h> # include <algorithm> # include <string.h&g ...
- Systrace
在构造函数,修复bug.完成代码后,你应该花一些时间来专注于应用性能.应用视频像素和运行的运行速度和流畅度影响用户体验. Android应用执行在一个共享资源的环境中,你的应用的性能会 ...
- 《Java并发编程实战》第十四章 构建自己的同步工具定义 札记
一.状态依赖性的管理 有界缓存实现的基类 @ ThreadSafe public abstract class BaseBoundedBuffer<E> { @GuardeBy( &quo ...
- Nyoj 修路方案(次小生成树)
描述 南将军率领着许多部队,它们分别驻扎在N个不同的城市里,这些城市分别编号1~N,由于交通不太便利,南将军准备修路. 现在已经知道哪些城市之间可以修路,如果修路,花费是多少. 现在,军师小工已经找到 ...
- Node.js v0.10.31API手工-DNS
原版的API品种,这是从以前的翻译和翻译风格不同 Node.js v0.10.31API手冊-文件夹 DNS 使用 require('dns') 引入此模块. dns 模块中的全部方法都使用了 C-A ...
- SpringMVC1
itRed You are never too old to set another goal or to dream a new dream. SpringMVC一路总结(一) SpringMVC听 ...