多线程同步(Lock)
来源:互联网 发布:域名服务器设置 编辑:程序博客网 时间:2024/05/17 22:24
实例
模仿生产者和消费者的模式
先来个两个线程的
class ProducerConsumerDemo{public static void main(String[] args){Res r=new Res();Pro in=new Pro(r);Cou out=new Cou(r);Thread t1=new Thread(in);Thread t2=new Thread(out);t1.start();t2.start();}}class Res{private String name;private int count=1;private boolean flag=false;public synchronized void set(String name){if(flag)try{wait();}catch(Exception e){}this.name=name+"---"+count++;System.out.println(Thread.currentThread().getName()+"+++生产者+++"+this.name);flag=true;this.notify();}public synchronized void out(){if(!flag)try{wait();}catch(Exception e){}System.out.println(Thread.currentThread().getName()+"消费者"+this.name);flag=false;this.notify();}}class Pro implements Runnable{private Res r;Pro(Res r){this.r=r;}public void run(){while(true){r.set("--商品--");}}}class Cou implements Runnable{private Res r;Cou(Res r){this.r=r;}public void run(){while(true){r.out();}}}
这样就OK了
下面就试试四个线程的,两个线程生产,两个线程消费;
class ProducerConsumerDemo{public static void main(String[] args){Res r=new Res();Pro in=new Pro(r);Cou out=new Cou(r);Thread t1=new Thread(in);Thread t2=new Thread(in);Thread t3=new Thread(out);Thread t4=new Thread(out);t1.start();t2.start();t3.start();t4.start();}}class Res{private String name;private int count=1;private boolean flag=false;public synchronized void set(String name){if(flag)try{wait();}catch(Exception e){}this.name=name+"---"+count++;System.out.println(Thread.currentThread().getName()+"+++生产者+++"+this.name);flag=true;this.notify();}public synchronized void out(){if(!flag)try{wait();}catch(Exception e){}System.out.println(Thread.currentThread().getName()+"消费者"+this.name);flag=false;this.notify();}}class Pro implements Runnable{private Res r;Pro(Res r){this.r=r;}public void run(){while(true){r.set("--商品--");}}}class Cou implements Runnable{private Res r;Cou(Res r){this.r=r;}public void run(){while(true){r.out();}}}
但结果是多生产少消费,并不是我们想要的一生产一消费;
改良代码
class ProducerConsumerDemo{public static void main(String[] args){Res r=new Res();Pro in=new Pro(r);Cou out=new Cou(r);Thread t1=new Thread(in);Thread t2=new Thread(in);Thread t3=new Thread(out);Thread t4=new Thread(out);t1.start();t2.start();t3.start();t4.start();}}class Res{private String name;private int count=1;private boolean flag=false;public synchronized void set(String name){while(flag)try{wait();}catch(Exception e){}this.name=name+"---"+count++;System.out.println(Thread.currentThread().getName()+"+++生产者+++"+this.name);flag=true;this.notifyAll();}public synchronized void out(){while(!flag)try{wait();}catch(Exception e){}System.out.println(Thread.currentThread().getName()+"消费者"+this.name);flag=false;this.notifyAll();}}class Pro implements Runnable{private Res r;Pro(Res r){this.r=r;}public void run(){while(true){r.set("--商品--");}}}class Cou implements Runnable{private Res r;Cou(Res r){this.r=r;}public void run(){while(true){r.out();}}}
对于多个生产者和消费者。
为什么要定义while判断标记。
原因:让被唤醒的线程在一次判断标记。
为什么定义notifyAll?
因为需要唤醒对方线程。
以为只用notify,容易出现只唤醒本方线程的情况,导致程序中的所有线程都等待。
JDK新版中提供了多线程升级解决方案。
将同步Syschronized替换成现实Lock操作;
将Object中的wait, notify notifyAll,替换了Condition对象。
该对象可以Lock锁 进行获取。
实现了本方只唤醒对方的操作;
import java.util.concurrent.locks.*;class ProducerConsumerDemo2{public static void main(String[] args){Res r=new Res();Pro in=new Pro(r);Cou out=new Cou(r);Thread t1=new Thread(in);Thread t2=new Thread(in);Thread t3=new Thread(out);Thread t4=new Thread(out);t1.start();t2.start();t3.start();t4.start();}}class Res{private String name;private int count=1;private boolean flag=false;private Lock lock=new ReentrantLock();private Condition condition_in=lock.newCondition();private Condition condition_out=lock.newCondition();public void set(String name)throws InterruptedException{lock.lock();try{while(flag)condition_in.await();this.name=name+"---"+count++;System.out.println(Thread.currentThread().getName()+"+++生产者+++"+this.name);flag=true;condition_out.signal();}finally{lock.unlock();//解锁对象一定要执行}}public void out()throws InterruptedException{lock.lock();try{while(!flag)condition_out.await();System.out.println(Thread.currentThread().getName()+"消费者"+this.name);flag=false;condition_in.signal();}finally{lock.unlock();}}}class Pro implements Runnable{private Res r;Pro(Res r){this.r=r;}public void run(){try{while(true){r.set("--商品--");}}catch (InterruptedException e){}}}class Cou implements Runnable{private Res r;Cou(Res r){this.r=r;}public void run(){try{while(true){r.out();}}catch (InterruptedException e){}}}
2 0
- 多线程同步(Lock)
- NET多线程同步方法:互斥锁(Lock)
- 利用lock实现多线程同步
- 使用Lock实现多线程同步
- 【多线程】多线程同步之synchronized和Lock
- c# 中多线程同步Lock的用法
- java多线程之Lock线程同步
- Java多线程同步之Lock用法
- 多线程:C#线程同步lock,Monitor,Mutex,同步事件和等待句柄(中) (转)
- 多线程:C#线程同步lock,Monitor,Mutex,同步事件和等待句柄(下) (转)
- Java:多线程,线程同步,同步锁(Lock)的使用(ReentrantLock、ReentrantReadWriteLock)
- 多线程:C#线程同步lock,Monitor,Mutex,同步事件和等待句柄(上)(转)
- 多线程:C#线程同步lock,Monitor,Mutex,同步事件和等待句柄(上)
- 多线程:C#线程同步lock,Monitor,Mutex,同步事件和等待句柄(中)
- 多线程:C#线程同步lock,Monitor,Mutex,同步事件和等待句柄(下)
- 多线程:C#线程同步lock,Monitor,Mutex,同步事件和等待句柄(上)
- 多线程:C#线程同步lock,Monitor,Mutex,同步事件和等待句柄(中)
- 多线程:C#线程同步lock,Monitor,Mutex,同步事件和等待句柄(下)
- Mac OS X下的网络编程(socket)的机制
- mysql利用binlog和mysqldump文件恢复数据库
- NB programmers
- tomcat改命令窗口标题的方法
- struts1和struts2的几点区别
- 多线程同步(Lock)
- 一个健壮的MD5算法
- LeetCode: Reverse Nodes in k-Group
- spark中stream编程指导(二)
- Cocos2d-x制作跳棋第四步:胜负判断、AI具体实现及实现特殊组合动作的小技巧
- HDU 2031
- 黑马程序员——Java基础---正则表达式
- 二叉树的遍历本质 - 前序,中序,后序
- PowerDesigner使用时经常报打印错误,提示安装打印机