多线程生产者消费者问题

一般面试喜欢问些线程的问题,较基础的问题无非就是死锁,生产者消费者问题,线程同步等等,在前面的文章有写过死锁,这里就说下多生产多消费的问题了

import java.util.concurrent.locks.*;

class BoundedBuffer {
   final Lock lock = new ReentrantLock();//对象锁
   final Condition notFull  = lock.newCondition(); //生产者监视器
   final Condition notEmpty = lock.newCondition(); //消费者监视器

   //资源对象
   final Object[] items = new Object[10];
   //putptr生产者角标,takeptr消费者角标,count计数器(容器的实际长度)
   int putptr, takeptr, count;

   public void put(Object x) throws InterruptedException {
       //生产者拿到锁
     lock.lock();
     try {
         //当实际长度不满足容器的长度
       while (count == items.length) 
           //生产者等待
         notFull.await();
       //把生产者产生对象加入容器
       items[putptr] = x; 
       System.out.println(Thread.currentThread().getName()+"     put-----------"+count);
       Thread.sleep(1000);
       //如果容器的实际长==容器的长,生产者角标置为0
       if (++putptr == items.length) putptr = 0;
       ++count;
       //唤醒消费者
       notEmpty.signal();
     } finally {
         //释放锁
       lock.unlock();
     }
   }

   public Object take() throws InterruptedException {
     lock.lock();
     try {
       while (count == 0) 
           //消费者等待
         notEmpty.await();
       Object x = items[takeptr]; 
       System.out.println(Thread.currentThread().getName()+"     get-----------"+count);
       Thread.sleep(1000);
       if (++takeptr == items.length) takeptr = 0;
       --count;
       //唤醒生产者
       notFull.signal();
       return x;
     } finally {
         //释放锁
       lock.unlock();
     }
   } 
 }

class Consu implements Runnable{
    BoundedBuffer bbuf;

    public Consu(BoundedBuffer bbuf) {
        super();
        this.bbuf = bbuf;
    }

    @Override
    public void run() {
        while(true){
        try {
            bbuf.take() ;
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }
    }

}
class Produ implements Runnable{
    BoundedBuffer bbuf;
    int i=0;
    public Produ(BoundedBuffer bbuf) {
        super();
        this.bbuf = bbuf;
    }

    @Override
    public void run() {
        while(true){
            try {
                bbuf.put(new String(""+i++)) ;
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}



//主方法
class Lock1{
    public static void main(String[] args) {
        BoundedBuffer bbuf=new BoundedBuffer();
        Consu c=new Consu(bbuf);
        Produ p=new Produ(bbuf);
        Thread t1=new Thread(p);
        Thread t2=new Thread(c);
        t1.start();
        t2.start();
        Thread t3=new Thread(p);
        Thread t4=new Thread(c);
        t3.start();
        t4.start();
    }
}


这个是jdk版本1.5以上的多线程的消费者生产者问题,其中优化的地方是把synchronized关键字进行了步骤拆分,对对象的监视器进行了拆离,synchronized同步,隐式的建立1个监听,而这种可以建立多种监听,而且唤醒也优化了,之前如果是synchronized方式,notifyAll(),在只需要唤醒消费者或者只唤醒生产者的时候,这个notifyAll()将会唤醒所有的冻结的线程,造成资源浪费,而这里只唤醒对立方的线程。代码的解释说明,全部在源码中,可以直接拷贝使用。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值