多线程2
2015-08-06 16:15
281 查看
多线程之间的通信,等待唤醒机制
概念:多个线程,不同的任务,处理同一资源。
多生产者,多消费者的问题。
if判断标记,只有一次,会导致不该运行的线程运行了。出现了数据错误的情况。
while判断标记,解决了线程获取执行权后,是否要运行!
notify:只能唤醒一个线程,如果本方唤醒了本方,没有意义。而且while判断标记+notify会导致死锁。
notifyAll解决了本方线程一定会唤醒对方线程的问题。
概念:多个线程,不同的任务,处理同一资源。
多生产者,多消费者的问题。
if判断标记,只有一次,会导致不该运行的线程运行了。出现了数据错误的情况。
while判断标记,解决了线程获取执行权后,是否要运行!
notify:只能唤醒一个线程,如果本方唤醒了本方,没有意义。而且while判断标记+notify会导致死锁。
notifyAll解决了本方线程一定会唤醒对方线程的问题。
class Resource { private String name; private int count = 1; private boolean flag = false; public synchronized void set(String name) { while(flag) try{this.wait();}catch(InterruptedException e){} this.name = name + count; count++;//2 3 4 System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name); flag = true; notifyAll(); } public synchronized void out() { while(!flag) try{this.wait();}catch(InterruptedException e){} System.out.println(Thread.currentThread().getName()+"...消费者........"+this.name); flag = false; notifyAll(); } } class Producer implements Runnable { private Resource r; Producer(Resource r) { this.r = r; } public void run() { while(true) { r.set("烤鸭"); } } } class Consumer implements Runnable { private Resource r; Consumer(Resource r) { this.r = r; } public void run() { while(true) { r.out(); } } }JDK1.5以后出现了更好的方案,Lock接口替代了synchronized ,Condition接口替代了Object中的监视方法,并将监视器方法封装成了Condition。和以前不同的是,以前一个锁上只能有一组监视器方法。现在,一个Lock锁上可以多组监视器方法对象。可以实现一组负责生产者,一组负责消费者
import java.util.concurrent.locks.*; class Resource { private String name; private int count = 1; private boolean flag = false; // 创建一个锁对象。 Lock lock = new ReentrantLock(); //通过已有的锁获取该锁上的监视器对象。 // Condition con = lock.newCondition(); //通过已有的锁获取两组监视器,一组监视生产者,一组监视消费者。 Condition producer_con = lock.newCondition(); Condition consumer_con = lock.newCondition(); public void set(String name)// t0 t1 { lock.lock(); try { while(flag) // try{lock.wait();}catch(InterruptedException e){} try{producer_con.await();}catch(InterruptedException e){} this.name = name + count; count++;//2 3 4 System.out.println(Thread.currentThread().getName()+"...生产者5.0..."+this.name); flag = true; consumer_con.signal(); } finally { lock.unlock(); } } public void out()// t2 t3 { lock.lock(); try { while(!flag) try{cousumer_con.await();}catch(InterruptedException e){} System.out.println(Thread.currentThread().getName()+"...消费者.5.0......."+this.name); flag = false; producer_con.signal(); } finally { lock.unlock(); } } } class Producer implements Runnable { private Resource r; Producer(Resource r) { this.r = r; } public void run() { while(true) { r.set("烤鸭"); } } } class Consumer implements Runnable { private Resource r; Consumer(Resource r) { this.r = r; } public void run() { while(true) { r.out(); } } }
相关文章推荐
- ZOJ 1671 Walking ant(bfs)
- sql联合查询inner join
- JDK各个版本的新特性jdk1.5-jdk8
- Android Data Binding高级用法-Observable、动态生成Binding Class(三)
- Implement Stack using Queues
- 使用Thread+Handler实现非UI线程更新UI界面
- apk反编译详解
- Num 22 : NYOJ : 0055 懒省事的小明 [ 优先队列 ]
- echarts 图表 展示 练习二 (ajax 后台加载数据)
- Codeforces Round #Pi (Div. 2)
- Android Data Binding高级用法-Observable、动态生成Binding Class(三)
- 翻转单链表
- C# 获得Excel工作簿Sheet页面(工作表)集合的名称
- hdu 1274 展开字符串
- ThinkPHP的RBAC权限控制
- 杂了个谈(一)——博客同名GitHub已开通
- crontab 执行scp.exp 失败
- OC语言中的通知
- js运算符|| &&
- MyEclipse 创建基于maven的Web Project工程的若干问题解决方法