Java生产者与消费者

要实现生产者消费者模式,容器是必不可少的。那么方法有很多,可以定义一个阻塞队列来作为容器使用

BlockingQueue<String> queue = new LinkedBlockingQueue<>(10);

阻塞队列的好处在与,不需要我们手动去将线程wait或者notify,当队列满或空时,自动将生产线程或消费线程wait,反之则notify。而如果用阻塞队列的话,许多知识我们无法去深刻的理解,所以我们可以自己手写一个容器。

首先,自定义一个容器类 Container.java

package producer;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author wxx
 * @date 2018/6/6 17:21
 */
public class Container {
    private int count = 0;
    public synchronized void produce(){
        int maxCount = 10;
        try {
            while (count >= maxCount){
                System.out.println("已满,生产等待," + Thread.currentThread() + "挂起");
                this.wait();
            }
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        count++;
        System.out.println(Thread.currentThread() + "生产线程生产了1条记录,目前记录数为:"+count);
        this.notifyAll();
        try{
            Thread.sleep(10);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public synchronized void consumer(){
        System.out.println(Thread.currentThread() + "进入消费");
        try {
            while (count <= 0){
                System.out.println("已空,消费等待," + Thread.currentThread() + "挂起");
                this.wait();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        count--;
        System.out.println(Thread.currentThread() +"消费线程消费了1条记录,目前记录数为: "+ count);
        this.notifyAll();    
        try{
            Thread.sleep(10);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws InterruptedException{
        Container container = new Container();
        Producer producer1 = new Producer(container);
        Producer producer2 = new Producer(container);
        Producer producer3 = new Producer(container);
        Consumer consumer1 = new Consumer(container);
        Consumer consumer2 = new Consumer(container);
        Consumer consumer3 = new Consumer(container);
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(producer1);
        executorService.execute(producer2);
        executorService.execute(producer3);
        executorService.execute(consumer1);
        executorService.execute(consumer2);
        executorService.execute(consumer3);
        Thread.sleep(3 * 1000);

        producer1.stop();
        producer2.stop();
        producer3.stop();
        consumer1.stop();
        consumer2.stop();
        consumer3.stop();
        executorService.shutdown();
    }
}

从上面我们可以看到,容器类里面有两个方法,分别为生产和消费的方法。并启动生产者和消费者线程个三个放入线程池中

package producer;

/**
 * @author wxx
 * @date 2018/6/6 18:56
 */
public class Consumer implements Runnable {
    private boolean flag = true;

    private Container container;

    Consumer(Container container){
        this.container = container;
    }
    @Override
    public void run(){
        while (flag){
            try {
                container.consumer();
            }catch (Exception e){
                e.printStackTrace();
            }

        }
    }
    public void stop(){
        this.flag = false;
    }
}

 

package producer;

/**
 * @author wxx
 * @date 2018/6/6 18:53
 */
public class Producer implements Runnable{
    private boolean flag = true;

    private Container container;

    Producer(Container container){
        this.container = container;
    }
    @Override
    public void run(){
        while (flag){
            try {
                container.produce();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
    public void stop(){
        this.flag = false;
    }
}

至此,已经全部结束了,下面说说我在做这个程序的时候,遇到的问题吧。对于大部分人来说,可能会很简单,就当做是一次分享吧。

第一次写的时候,把

while (count >= maxCount){
                System.out.println("已满,生产等待," + Thread.currentThread() + "挂起");
                this.wait();
            }
while (count <= 0){
                System.out.println("已空,消费等待," + Thread.currentThread() + "挂起");
                this.wait();
            }

这两部分里面的while写成了if,那会出现什么问题呢?

我们可以看到,里面竟然出现了负数,其实许多人应该一眼就能看出来,我也做下解释吧。我们考虑下这种情况:当容器里面的产品为0时,此时3个消费者同时来消费,毫无悬念,他们都被挂起了,而此时,有一个生产者生产了一个产品,唤醒了所有消费者。而消费线程只管往下执行,就是从

 
System.out.println("已空,消费等待," + Thread.currentThread() + "挂起");
this.wait();

这后面执行下去,并没有进行产品数量的判断,三次连续执行,便出现了负数。而如果用while的话,他会自动回滚判断,产品数量为0时继续挂起。

那么,用if来实现难道就不行了么?

当然可以,那就是把

 

 

count--;

System.out.println(Thread.currentThread() +"消费线程消费了1条记录,目前记录数为: "+ count);

this.notifyAll();

count++;
System.out.println(Thread.currentThread() + "生产线程生产了1条记录,目前记录数为:"+count);
this.notifyAll();

这两段代码放到else里,这样就不会执行到这两段代码了。不过这样做的后果就是,当被挂起的线程被唤醒时,不管条件是否满足,他们都不会执行生产或者消费的行为。也就是说,只要线程一旦被挂起,就等于次线程不会有实际行为了。

当并发量高的时候,我们可以考虑使用AtomicInteger类,一个线程安全的计数器。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值