java基础七---线程2 生产者和消费者方式二

第一步:创建实体对象

/**
 * 商品实体类
 */

public class GoodsEntity {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

 

 

第二步:创建生产线程

/**
 * 生产者线程
 */
public class ProducerThread implements Runnable{
    public  boolean isRuning=false;
    GoodsEntity goodsEntity =null;//商品实体对象
    int id=0; //商品id
    ReentrantLock reentrantLock;
    Condition proCondition;//生产线程的Condition
    Condition consCondition; //消费线程的Condition
    public ProducerThread(GoodsEntity goodsEntity,ReentrantLock reentrantLock,Condition proCondition,Condition consCondition){
        this.goodsEntity=goodsEntity;
        this.reentrantLock=reentrantLock;
        this.proCondition=proCondition;
        this.consCondition=consCondition;
    }

    @Override
    public void run() {
       while (isRuning) {
           reentrantLock.lock();//同步锁
           try {
               Thread.sleep(100);
               if (goodsEntity.getName() == null) {    //name 为空说明已经消费完了需要生产商品
                   goodsEntity.setName("商品" + (++id));
                   Log.e("Thread_test_pro", Thread.currentThread().getName() + " 生产商品:---" + goodsEntity.getName());
                   //商品生产完成,通知消费线程消费
                   consCondition.signalAll();
               } else {
                   //name  不为空说明商品还没有消费完,等待商品消费
                   proCondition.await();
               }
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
           reentrantLock.unlock();//解锁
       }
    }
}

第三步:创建消费线程

/**
 * 消费者线程
 */

public class ConsumerThread implements Runnable{
    public  boolean isRuning=false;
    private GoodsEntity goodsEntity=null;
    int count=0;
    ReentrantLock reentrantLock;
    Condition proCondition;//生产线程的Condition
    Condition consCondition; //消费线程的Condition

    public ConsumerThread(GoodsEntity goodsEntity, ReentrantLock reentrantLock,Condition proCondition, Condition consCondition){
        this.goodsEntity=goodsEntity;
        this.reentrantLock=reentrantLock;
        this.proCondition=proCondition;
        this.consCondition=consCondition;
    }
    @Override
    public void run() {
        while (isRuning){
            reentrantLock.lock();
                try {
                    if(goodsEntity.getName()==null){
                         //商品为空,说明没有生产新的商品,等待生产
                        consCondition.await();
                    }else{
                        Log.e("Thread_test_con", Thread.currentThread().getName()+ " 消费商品:" + goodsEntity.getName());
                        goodsEntity.setName(null);//清空商品
                        //商品消费完之后清空商品,通知生产
                        proCondition.signalAll();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            reentrantLock.unlock();
        }
    }
}

第四步:测试

   public static void main(){
        GoodsEntity goodsEntity=new GoodsEntity();
        ReentrantLock reentrantLock=new ReentrantLock();
        Condition proCondition=reentrantLock.newCondition();//生产线程的Condition
        Condition consCondition=reentrantLock.newCondition(); //消费线程的Condition
        ProducerThread producerThread=new ProducerThread(goodsEntity,reentrantLock,proCondition,consCondition);//生产者
        ConsumerThread consumerThread=new ConsumerThread(goodsEntity,reentrantLock,proCondition,consCondition);//消费者
        producerThread.isRuning=true;
        consumerThread.isRuning=true;

        Thread proThrad1=new Thread(producerThread,"生产线程1");
        Thread proThrad2=new Thread(producerThread,"生产线程2");
        Thread proThrad3=new Thread(producerThread,"生产线程3");

        Thread conThrad1=new Thread(consumerThread,"消费线程1");
        Thread conThrad2=new Thread(consumerThread,"消费线程2");
        Thread conThrad3=new Thread(consumerThread,"消费线程3");

        proThrad1.start();
        proThrad2.start();
        proThrad3.start();

        conThrad1.start();
        conThrad2.start();
        conThrad3.start();
        try {
            Thread.sleep(6000);
            producerThread.isRuning=false;
            consumerThread.isRuning=false;
            Log.e("Thread_test", "proThrad1.isAlive()="+proThrad1.isAlive());
            Thread.sleep(2000);
            Log.e("Thread_test", "proThrad1.isAlive()2="+proThrad1.isAlive());
        } catch (InterruptedException e) {
            Log.e("Thread_test", "InterruptedException ex="+e.toString());
            e.printStackTrace();
        }
    }

 

备注: signalAll()调用之后会自动退出同步锁

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值