生产者-消费者模型Java代码实现

转载请注明出处
作者:@LSZJXin
由于本人水平有限,如有不正确的地方欢迎留言指出,谢谢!

考查Java的并发编程时,手写”生产者-消费者模型”是一个经典问题。本文给出一种相对比较简单容易理解、容易实现的java代码实现这一模型。

步骤:
1. 创建一个实体类(本例为WoTou)
2. 创建一个”容器”
3. 创建生产者线程类
4. 创建消费者线程类

5. main()函数创建一个或多个生产者以及消费者线程,并同时运行

  1. 首先创建一个实体类

class WoTou{

    //用来标记生产的每个实例
    int id;

    public WoTou(int id){
        this.id = id;
    }

    public String toString(){
        //重写toString方法,方便打印
        return "WoTou:" + id;
    }
}
  1. 创建一个“容器”,用栈这一数据结构实现,生产者每次从将生产的物品放到最上边,同时消费者每次消费最上边的物品。(也可以用循环队列这一数据结构实现)
class Container{

    int index = 0;
    WoTou []arrWT = new WoTou[6];

    //通过synchronized这一关键字来同步
    public synchronized void set(WoTou wotou){//生产方法,加锁

        while(index == arrWT.length){
            //必须使用while,而不是if
            //因为如果用if抛出异常,后边的代码还会继续执行
            //while的话抛出异常后还是回到while里边执行
            try {
                this.wait();
            } catch ( InterruptedException e) {
                e.printStackTrace();
            }
        }

        //唤醒其他线程
        this.notifyAll();

        arrWT[index++] = wotou;

    }

    //通过synchronized这一关键字来同步
    public synchronized WoTou get(){//消费方法,加锁

        while(index == 0){
        //同理,必须使用while
            try{
                this.wait();
            }catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        this.notifyAll();


        WoTou woTou = arrWT[--index];
        return woTou;
    }

}
  1. 创建生产者线程
class Producer implements Runnable{//生产者线程

    Container container = null;

    public Producer(Container container) {
        this.container = container;
    }

    @Override
    public void run() {

        for(int i=0;i<100;i++){

            WoTou wotou = new WoTou(i);
            container.set(wotou);
            System.out.println("生产了:" + wotou);
            try{
                Thread.sleep((int)(Math.random() * 200));
            }catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
  1. 创建消费者线程
class Consumer implements Runnable{//消费者线程

    Container container = null;

    public Consumer(Container container) {
        this.container = container;
    }

    @Override
    public void run() {

        while(true){

            WoTou woTou = container.get();
            System.out.println("消费了:"+woTou);

            try {
                Thread.sleep((int)(Math.random() * 1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
  1. 主函数创建线程并运行
public class ProducerConsumerTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Container container = new Container();
        Producer p = new Producer(container);
        Consumer c = new Consumer(container);
        new Thread(p).start();

        //可以创建多个生产者消费者线程
        //new Thread(p).start();
        //new Thread(p).start();
        //new Thread(c).start();
        new Thread(c).start();
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_35985385/article/details/80967737