java多线程学习(五)---生产者消费者替换方案

java 在1.5之后提供了多线程升级解决方案

 其中Synchronized被接口Lock所代替
 Object中的wait notify notifyAll被 Condition接口中的方法所替代。
Lock类的介绍:----其中,Lock 替代了 synchronized 方法和语句的使用
  lock:获取锁
  unlock:释放锁
  newCondition:返回Condition实例
Condition类的介绍:----Condition 替代了 Object 监视器方法的使用
  await() 是线程等待,类似于wait方法。需要抛
  signal() 唤醒一个等待的线程

  signalAll() 唤醒所有等待的线程

本例主要演示生产者、消费者这个经典案例的替换方案。

 并学习Lock和Condition的方法
 描述:生产者负责生产商品,消费者负责消费商品,其中生产者为空时不能,消费者不能进行消费。
 实现步骤
 1、定义一个操作类方法:Resources类,属性:name,商品编号 count.标志 flag.
  当flag为false时,表示生产者还未生成,这是需要执行生成方法。执行完后将flag设置为true。
并唤醒消费者去执行消费方法
 当flag为true时,表示生产者已经生产一个产品,执行等待(await)操作。此时,消费者执行完消
费操作之后,将flag设置为false,并唤醒生产者执行生产方法。

void set(String name)方法,void out();方法。
 2、生产者类:Producer实现Runnable接口,覆写run方法。Producer(Resources res),有参构造方法
 3、消费者类:Consumer实现Runnabel接口,覆写run方法。Consumer(Resources res),有参构造方法
 4、创建多个生产者、消费这线程。用来启动多线程

import java.util.concurrent.locks.*;
public class ProducerConsumer{
  public static void main(String args[]){
    Resources res = new Resources();
    Producer pro = new Producer(res);
    Consumer con = new Consumer(res);
    Thread t1 = new Thread(pro);  //生产者..
    Thread t2 = new Thread(pro);  //生产者..
    Thread t3 = new Thread(pro);  //生产者..
    Thread t4 = new Thread(con);  //生产者..
    Thread t5 = new Thread(con);  //生产者..
    Thread t6 = new Thread(con);  //生产者..

    t1.start();
    t2.start();
    t3.start();
    t4.start();
    t5.start();
    t6.start();
  }
}
class Resources{
  private String name;
  private int count=1;
  private boolean flag;

  private final Lock lock = new ReentrantLock();  //创建一个Lock的实例
  private Condition condition_Pro = lock.newCondition(); //创建一个控制Producer线程的Condition实例
  private Condition condition_Con = lock.newCondition();//创建一个控制Consumer线程的condition实例

  public void set(String name)throws InterruptedException{
    lock.lock();  //开启锁
    try{
      while(flag)  //需要持续判断锁,如果为true,需要等待
        condition_Pro.await();   //Producer线程等待。

        this.name = name+"--"+(++count);
        System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name);
        flag = true;
        condition_Con.signal(); //唤醒Customer的线程
    }finally{
      lock.unlock();   //无论程序是否执行完,都要释放资源
    }

  }
  public void out()throws InterruptedException{
    lock.lock();
    try{
      while(!flag)  //当flag为false时,没有生成,消费者需要等待
        condition_Con.await();  //消费者线程等待

        System.out.println(Thread.currentThread().getName()+".......消费者........."+count);
        flag = false;
        condition_Pro.signal();  //唤醒Producer的线程
    }finally{
      lock.unlock();
    }

  }
}
class Producer implements Runnable{
  Resources res;
  public Producer(Resources res){
    this.res = res;
  }
    public void run(){
        //重复执行生产的方法
          try{
            while(true)
              res.set("lzl");
          }catch(InterruptedException e){}
    }
}
class Consumer implements Runnable{
  Resources res;
  public Consumer(Resources res){
    this.res = res;
  }
    public void run(){
        //重复执行消费的方法
        try{
          while(true)
            res.out();
        }catch(InterruptedException e){}
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值