Java线程通信-生产者与消费者简单实例(二)


1.产品类

//产品
public class Product {
    public Product(){
        System.out.print("生产了一个产品");
    }
}

2.生产者类

//生产者
public class Producer implements Runnable{
    private int COUNT_MAX;               //仓库大小
    private LinkedList<Product> list;   //仓库
    private Lock lock;
    private Condition c;
    private Condition p;
    public Producer(Consumer consumer,int COUNT_MAX){
        this.COUNT_MAX = COUNT_MAX;
        this.list = consumer.getList();
        this.lock = consumer.getLock();
        this.c = consumer.getC();
        this.p = consumer.getP();
    }

    @Override
    public void run() {
        //不断的生产产品放入仓库
        for(;;){
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            lock.lock();
            try{
                while(list.size() >= COUNT_MAX){     //仓库数量大于COUNT_MAX时等待
                    try {
                        p.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                list.push(new Product());
                System.out.println(",还剩下: "+list.size()+"个");
                c.signal();   //唤醒消费者消费
            }finally{
                lock.unlock();
            }
        }
    }
}

3.消费者类

//消费者
public class Consumer implements Runnable {
    private LinkedList<Product> list = new LinkedList<Product>();
    private Lock lock = new ReentrantLock();
    private Condition c = lock.newCondition();
    private Condition p = lock.newCondition();

    public LinkedList<Product> getList() {
        return list;
    }
    public Lock getLock() {
        return lock;
    }
    public Condition getC() {
        return c;
    }
    public Condition getP() {
        return p;
    }

    @Override
    public void run() {
        for(;;){
            try {
                TimeUnit.MILLISECONDS.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //不断从仓库消费产品
            lock.lock();
            try{
                while(list.size() <= 0){//仓库没产品时等待
                    try {
                        c.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                list.pop();
                System.out.println("消费了一个产品,剩: "+list.size()+"个");
                p.signal();   //消费产品后唤醒生产者
            }finally{
                lock.unlock();
            }

        }
    }
}

4.测试

@Test
public void Test(){
    Consumer consumer = new Consumer();
    Producer producer = new Producer(consumer,10);
    new Thread(consumer).start();
    new Thread(consumer).start();
    new Thread(producer).start();
    new Thread(producer).start();
    new Thread(producer).start();
    new Thread(producer).start();
    try {
        Thread.sleep(100);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

文章作者: Bryson
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Bryson !
评论
 上一篇
下一篇 
  目录