- 浏览: 277942 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
haiyangyiba:
jimichan 写道果然3.1.0.M2不行,切换到 3.1 ...
spring 使用注解装配的Bean如何使用property-placeholder属性配置中的值 -
lishl:
非常不错。用心之作。谢谢。
Jetty6 指南书 第4章 Jetty服务配置文件 -
Yinny:
楼主V5 网上的文章一搜一堆互相copy的,自己写的文章 太难 ...
Jetty6 指南和解析 - 第二章 Jetty初探 -
290845534:
wangpeihu 写道我想收藏,可是我不敢,我没有那500W ...
Jetty6 指南和解析 - 第二章 Jetty初探 -
宋建勇:
对于我们这些菜鸟来说,你突来停稿了,很伤脑筋啊,希望楼主继续! ...
时间飞快,jetty7已经发展成熟
第一部分 集合 http://jimichan.iteye.com/blog/951948
第二部分 线程池 http://jimichan.iteye.com/blog/951950
第三部分 锁 http://jimichan.iteye.com/blog/951954
第四部分 同步辅助类 http://jimichan.iteye.com/blog/951955
Concurrent In Java,第四部分 同步辅助类
2011-3-9 延昭 & 陈汝烨 版权所有,特别禁止发布到百度文库
这篇是来自公司内部分享会议是写的总结,有些内容没有表达出来,大家可以来踩,但是需留下原因,以便后续补充。
第一部分 集合 http://jimichan.iteye.com/blog/951948
第二部分 线程池 http://jimichan.iteye.com/blog/951950
第三部分 锁 http://jimichan.iteye.com/blog/951954
第四部分 同步辅助类 http://jimichan.iteye.com/blog/951955
4. 同步辅助类
你提交了一些任务,但你想等它们都完成了再做另外一些事情;你提交了一些任务,但是不想让它们立刻执行,等你喊123开始的时候,它们才开始执行;等等这些场景,线程之间需要相互配合,或者等待某一个条件成熟执行。这些场景想你就需要用到同步辅助类。
4.1 CountDownLatch
CountDownLatch 内部有个计数器,通过构造函数来指定。这个类就好比是倒计时的电子牌,当倒计时为0的时候就可以一起做一些事情。
摘自JavaDoc的方法介绍
void |
await() 使当前线程在锁存器倒计数至零之前一直等待,除非线程被中断。 |
boolean |
await(long timeout, TimeUnit unit) 使当前线程在锁存器倒计数至零之前一直等待,除非线程被中断或超出了指定的等待时间。 |
void |
递减锁存器的计数,如果计数到达零,则释放所有等待的线程。 |
long |
getCount() 返回当前计数。 |
摘自JavaDoc的例子
class Driver { // ...
void main() throws InterruptedException {
CountDownLatch startSignal = new CountDownLatch(1);
CountDownLatch doneSignal = new CountDownLatch(N);
for (int i = 0; i < N; ++i) // create and start threads
new Thread(new Worker(startSignal, doneSignal)).start();
doSomethingElse(); // don't let run yet
startSignal.countDown(); // let all threads proceed
doSomethingElse();
doneSignal.await(); // wait for all to finish
}
}
class Worker implements Runnable {
private final CountDownLatch startSignal;
private final CountDownLatch doneSignal;
Worker(CountDownLatch startSignal, CountDownLatch doneSignal) {
this.startSignal = startSignal;
this.doneSignal = doneSignal;
}
public void run() {
try {
startSignal.await();
doWork();
doneSignal.countDown();
} catch (InterruptedException ex) {} // return;
}
void doWork() { ... }
}
当CountDownLatch(1)的时候,它就好比是个信号枪了。
4.2 CyclicBarrier
这个同步辅助类,它让多个线程可以在多个屏障点进行等待,所以叫cyclic,而且有个附加选择你可以在线程到达屏障点后执行一个任务(在释放其他线程之前)
new CyclicBarrier(N,
new Runnable() {
public void run() {
mergeRows(...);
}
});
为了帮助你理解,假设一个场景。
有一个任务,A、B、C分别从三个仓库(甲乙丙)搬运不同3个不同的零件到客户X的公司,然后再一起组装机器,完成后一起坐车去公司总部。
这个任务需要ABC三个线程同时进行,但是由于从仓库到客户X那边距离不等、交通状态未知的情况下,所花费的时间也不等。同时由于三个人负责的零件不同,所以安装机器的时候花费时间也不一样。这个场景中有两个需要线程间等待的地方。CyclicBarrier就可以闪亮登场了。
public class Main3 {
public static void main(String[] args) {
CyclicBarrier barrier = new CyclicBarrier(3,new Runnable() {
@Override
public void run() {
System.out.println("到达公共屏障点");
}
});
ExecutorService es = Executors.newCachedThreadPool();
es.submit(new Worker("A", 5000, 8000, barrier));
es.submit(new Worker("B", 2000, 16000, barrier));
es.submit(new Worker("C", 9000, 2000, barrier));
es.shutdown();
}
static class Worker implements Runnable {
String name;
int t1;// 搬运零件所需要的时间
int t2;// 参与组装工作需要的时间
CyclicBarrier barrier;
public Worker(String name, int t1, int t2, CyclicBarrier barrier) {
super();
this.name = name;
this.t1 = t1;
this.t2 = t2;
this.barrier = barrier;
}
@Override
public void run() {
try {
print(name + " 开始搬运零件");
Thread.sleep(t1);// 模拟搬运时间
print(name + " 到达目的地");
int a = barrier.await(); // 等待其他人
if(a==0){
//说明是最后一个到的可以执行特殊操作
}
print(name + " 开始组装机器");
Thread.sleep(t2);// 模拟组装时间.
print(name + " 完成组装机器");
barrier.await(); // 等待其他人组装完毕
print(name + " 一起回总公司");
} catch (Exception e) {
e.printStackTrace();
}
}
}
static SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
static void print(String x) {
System.out.println( sdf.format(new Date()) + ": "+x);
}
}
4.3 Semaphore
一个经典的信号量计数器。一般被用来控制对共享资源同时访问线程数量的控制。
特殊情况下信号量设置为1,那么就类似互斥锁的功能。
此类的构造方法可选地接受一个公平 参数。当设置为 false 时,此类不对线程获取锁的顺序做任何保证。和之前提到的争用获取顺序一样,在非公平模式下,系统将获得更好的吞吐量,jvm也会保证在非公平模式下让所有线程得到访问机会。
参考书目
JavaDoc http://www.oschina.net/uploads/doc/javase-6-doc-api-zh_CN/overview-summary.html
java.util.concurrent 您不知道的 5 件事
http://www.ibm.com/developerworks/cn/java/j-5things4.html?ca=drs-
http://www.ibm.com/developerworks/cn/java/j-5things15/index.html
发表评论
-
spring 使用注解装配的Bean如何使用property-placeholder属性配置中的值
2012-03-13 14:03 13263很久没动笔了 spring 使用注解装配的Bean如何 ... -
轻松配置log4j,实现错误消息的Gtalk通知消息到智能手机。
2011-07-06 18:11 1967在Android手机上装个Gtalk,接收Log4j的异常提醒 ... -
系统URL规划
2011-06-13 12:18 0一个好的系统URL规划,不仅可以使得URL美观、简单易懂,而且 ... -
干掉讨厌的commons-logging依赖
2011-06-13 11:42 6159因为使用Slf4j,所以一直以来都对commons-loggi ... -
Concurrent In Java 6 分享,第三部分 锁
2011-03-10 11:45 3876第一部分 集合 http://jimich ... -
Concurrent In Java 6 分享,第二部分 线程池
2011-03-10 11:44 6511第一部分 集合 http://jimich ... -
Concurrent In Java 6 分享,第一部分 集合
2011-03-10 11:43 6098第一部分 集合 http://jimichan.i ... -
Concurrent In Java 6 分享 你不一定都了解
2011-03-10 11:27 2097由于帖子字数限制无法完整发布,请访问博客吧 第一 ... -
spring 2.5 中文chm文档
2010-07-22 16:00 4192换了个电脑,想看下spring 2.5 的中文chm文档。 ... -
正确使用ThreadPoolExecutor
2010-02-04 10:23 2971一直都是使用Executors.new ... -
jetty自带的服务端代理(proxy)
2009-11-02 13:33 6926代理配置的文档 http://docs.codehaus.or ... -
在eclipse中 配置spring 自定义的schema文件
2009-04-08 16:24 6849问题:使用了spring自定义schema时,在xml文件中无 ... -
spring jdbc的批处理功能 ---- 当中途失败时
2009-03-19 16:12 2611spring JdbcTemplate有一个功能是batchU ... -
搜索java类路径中文件的方法
2009-03-10 11:10 1874问题: 搜索所有类路径中所有符合文件名规则的资源? 解决方 ... -
spring事务管理在mysql数据库无法回滚
2008-11-07 11:31 3509检查了半天,配置和程序都没有问题,就是无法回滚数据。 最后才 ... -
oracle jdbc链接异常解决一则
2008-09-09 14:03 1611在linux 下连接另外一个oracle数据库出错了 异常: ... -
用java好久,今天才知道初始化集合还可以这样写
2008-05-06 16:53 14780Set set = new HashSet() {{ ... -
请教关于使用PropertyOverrideConfigurer,怎么覆写 bean引用的问题
2008-01-07 16:46 1557大家都知道使用PropertyOverrideConfigur ... -
理解javascript的类构造函数
2007-09-05 13:13 3067我们经常使用new运行符去create ... -
Struts2 Wildcard Mappings用法之访问tiles定义
2007-08-23 16:19 2487哈哈,终于可以用struts2了。以前没用过webwork,几 ...
相关推荐
java同步大杀器concurrent 包java同步大杀器concurrent 包java同步大杀器concurrent 包java同步大杀器concurrent 包java同步大杀器concurrent 包java同步大杀器concurrent 包java同步大杀器concurrent 包java同步大...
Concurrent Programming in Java - Design Principles and Patterns
Concurrent Programming in Java Design Principles and Pattern英文版 2.48M Java并发编程设计原则与模式_第二版(原书中文版) 19.4M Concurrent_Programming_in_Java_Design_Principles_Lecture DougLea
Java Concurrent in practice (animated)
Concurrent - Programming in Java.pdf,ppt,Doug Lea
concurrent programming in java design principles and patterns .chm
Doug Lea, Concurrent Programming in Java Design Principles and Patterns
详细介绍java多线程编程的各个基础概念。JUC作者doug lea著
word版本的资料,网上...Concurrent Programming in Java™: Design Principles and Patterns, Second Edition Doug Lea Publisher: Addison Wesley Second Edition October 01, 1999 ISBN: 0-201-31009-0, 432 pages
This book provides an introduction to concurrent, real-time, distributed programming with Java object-oriented language support as an algorithm description tool. It describes in particular the ...
Java并发编程-设计原则与模式(Concurrent.Programming.in.Java-Design.Principles.and.Patterns(Second.Edition))(中英版)
Concurrent Programming in Java™: Design Principles and Patterns, Second Edition. 介绍并发编程的好的著作,著名的并发大师 Doug Lea的杰作。
Concurrent.and.Distributed.Computing.in.Java
Concurrent Programming in Java Thread 看看吧
This book shows readers how to use the Java platform's threading model more precisely by helping them to understand the patterns and tradeoffs associated with concurrent programming
1. java.util.concurrent - Java 并发工具包 2. 阻塞队列 BlockingQueue 3. 数组阻塞队列 ArrayBlockingQueue 4. 延迟队列 DelayQueue 5. 链阻塞队列 LinkedBlockingQueue 6. 具有优先级的阻塞队列 ...
资深Java专家10年经验总结,全程案例式讲解,首本全面介绍Java多线程编程技术的专著 结合大量实例,全面讲解Java多线程编程中的并发访问、线程间通信、锁等最难突破的核心技术与应用实践 封底 Java多线程无处不在,...
java concureent in practice java并发编程实战,从计算机底层执行过程深入并发解决方案,文档含有中英文两个版本。
资源JavaConcurrent实用知识库分享知识分享