线程间的通信_多生产者多消费者问题_JDK1.5新特性_Lock

对于同步代码块,对于锁的操作是隐式的
但是在JDK1.5之前的这种方法效率有点低,判断会很多,后面升级之后有新的解决方案

jdk1.5以后将同步和锁封装成了对象,并将操作锁的隐式方式定义到了该对象中,将隐式动作变成了显示动作

Lock接口:替代了同步代码块或者同步函数,将同步的隐式锁操作变成显示锁操作,同时更为灵活,可以一个锁上加上多组监视器

     lock():获取锁       unlock():释放锁,通常定义在finally代码块中

Condition接口:替代了Object中的wait notify notifyAll方法,将这些监视器方法单独封装,变成了Condition监视器对象,可以喝任意锁组合

        await();    singal();       singalAll();

同步代码块到lock的变化:

线程间的通信_多生产者多消费者问题_JDK1.5新特性_Lock

 import java.util.concurrent.locks.*;//lock在此包中
class LockDemo{
public static void main(String[] args) {
Resrouce r = new Resrouce();
Producer pro = new Producer(r);
Consumer con = new Consumer(r); Thread t1 = new Thread(pro);
Thread t2 = new Thread(pro);
Thread t3 = new Thread(con);
Thread t4 = new Thread(con); t1.start();
t2.start();
t3.start();
t4.start(); }
} class Resrouce{
private String name;
private int count = 1;
private boolean flag = false; Lock lock = new ReentrantLock();//创建一个锁对象 public void set(String name){
lock.lock();
try{
while(flag)
try{this.wait();}catch(Exception e){}
this.name = name+count;
count++;
System.out.println(Thread.currentThread().getName()+"...生产者.."+this.name);
flag = true;
notifyAll();
}finally{
lock.unlock();
} } public void out(){
lock.lock();
try{
while(!flag)
try{this.wait();}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+"...消费者.."+this.name);
flag = true;
notifyAll();
}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();
}
}
}

上述实例中锁换成了lock之后就不能用this来操作wait等方法了,应该用lock锁中的Condition接口中的方法,该接口将Object监视器方法(wait,notify,notifyAll)分解成不同的对象

 import java.util.concurrent.locks.*;
class LockDemo{
public static void main(String[] args) {
Resrouce r = new Resrouce();
Producer pro = new Producer(r);
Consumer con = new Consumer(r); Thread t1 = new Thread(pro);
Thread t2 = new Thread(pro);
Thread t3 = new Thread(con);
Thread t4 = new Thread(con); t1.start();
t2.start();
t3.start();
t4.start(); }
} class Resrouce{
private String name;
private int count = 1;
private boolean flag = false; Lock lock = new ReentrantLock(); //通过已有的锁获取该锁上的监视器对象,一个锁可以有多个监视器
Condition con = lock.newCondition(); public void set(String name){
lock.lock();
try{
while(flag)
try{con.await();}catch(Exception e){}
this.name = name+count;
count++;
System.out.println(Thread.currentThread().getName()+"...生产者.."+this.name);
flag = true;
con.signalAll(); }finally{
lock.unlock();
}
} public void out(){
lock.lock();
try{
while(!flag)
try{con.await();}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+"...消费者.."+this.name); flag = true;
con.signalAll();
}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();
}
}
}

上述实例只有一组监视器,既监视着生产者,又监视着消费者,可以搞两个监视器,一组监视生产者,一组监视消费者

 import java.util.concurrent.locks.*;
class LockDemo{
public static void main(String[] args) {
Resrouce r = new Resrouce();
Producer pro = new Producer(r);
Consumer con = new Consumer(r); Thread t1 = new Thread(pro);
Thread t2 = new Thread(pro);
Thread t3 = new Thread(con);
Thread t4 = new Thread(con); t1.start();
t2.start();
t3.start();
t4.start(); }
} class Resrouce{
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){
lock.lock();
try{
while(flag)
try{Producer_con.await();}catch(Exception e){}
this.name = name+count;
count++;
System.out.println(Thread.currentThread().getName()+"...生产者.."+this.name);
flag = true;
//con.signalAll();//会发生死锁,所以要用到notifyAll
Consumer_con.signa();//有了两组监视器后就不用signaAll方法唤醒所有线程池中的线程了,只唤醒对方的一个线程,这样效率就高很多了
}finally{
lock.unlock();
}
} public void out(){
lock.lock();
try{
while(!flag)
try{Consumer_con.await();}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+"...消费者.."+this.name); flag = true;
//con.signalAll(); 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();
}
}
}

wait和sleep的区别:

wait和sleep都是让线程处于冻结状态的方法
区别:
  1.wait可以指定时间也可以不指定。sleep必须指定时间。
  2.在同步中时,对CPU的执行权和锁的处理不同.wait:释放执行权,释放锁。sleep:释放执行权,不释放锁(不同中的代码必须拿到锁才能运行)

  3.wait,notify和notifyAll只能在同步控制方法或者同步控制块里面使用,而sleep可以在任何地方使用(使用范围
     synchronized(x){
          x.notify()
         //或者wait()
     }
      4.sleep必须捕获异常,而wait,notify和notifyAll不需要捕获异常

上一篇:【转】Javascript中理解发布--订阅模式


下一篇:[转载]JavaEE学习篇之——Session&&Cookie