多线程_多生产者多消费者的实现问题

Posted 大胡龙的小圈子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多线程_多生产者多消费者的实现问题相关的知识,希望对你有一定的参考价值。

  多生产者多消费者是Java中的一个经典的案例,采用等待唤醒机制来实现,代码如下:

  

public class ProducerConsumer {
    public static void main(String[] args) {
        Resource res=new Resource();
        Producer pro1=new Producer(res);
        Producer pro2=new Producer(res);
        Consumer con1=new Consumer(res);
        Consumer con2=new Consumer(res);
        Thread t1=new Thread(pro1);
        Thread t2=new Thread(pro2);
        Thread t3=new Thread(con1);
        Thread t4=new Thread(con2);
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }
}
class Resource {
    private int count;
    private boolean flag=true;
    public synchronized void produce()  {
        while(!flag) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(Thread.currentThread().getName()+"生产"+count+"烤鸭");
        count++;
        flag=false;
        this.notifyAll();
    }
    public synchronized void consume() {
        while(flag) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(Thread.currentThread().getName()+"消费"+count+"烤鸭");
        flag=true;
        this.notifyAll();
    }
}
class Producer implements Runnable{
    Resource r;
    
    public Producer(Resource r) {
        super();
        this.r = r;
    }

    @Override
    public void run() {
        while(true) {
            r.produce();
        }
    }
    
}
class Consumer implements Runnable {
    Resource r;
    public Consumer(Resource r) {
        super();
        this.r = r;
    }
    @Override
    public void run() {
        while(true) {
            r.consume();
        }
    }
    
}

  还有一种采用BlockQueue实现的多生产者多消费者模式:

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class ProducerConsumer {
    public static void main(String[] args) {
        Resource res=new Resource();
        BlockingQueue queue=new LinkedBlockingQueue(1);
        Producer pro1=new Producer(res,queue);
        Producer pro2=new Producer(res,queue);
        Consumer con1=new Consumer(res,queue);
        Consumer con2=new Consumer(res,queue);
        Thread t1=new Thread(pro1);
        Thread t2=new Thread(pro2);
        Thread t3=new Thread(con1);
        Thread t4=new Thread(con2);
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }
}
class Resource {
    int count;

    @Override
    public String toString() {
        return "Food [count=" + count + "]";
    }
    
}
class Producer implements Runnable{
    Resource r;
    BlockingQueue queue;
    
    public Producer(Resource r, BlockingQueue queue) {
        super();
        this.r = r;
        this.queue = queue;
    }

    @Override
    public void run() {
        while(true) {
            produce();
        }
    }

    public void produce() {
        try {
            System.out.println("生产"+r);
            queue.put(r);
            r.count++;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    
}
class Consumer implements Runnable {
    Resource r;
    BlockingQueue queue;
    
    public Consumer(Resource r, BlockingQueue queue) {
        super();
        this.r = r;
        this.queue = queue;
    }
    @Override
    public void run() {
        while(true) {
            consume();
        }
    }
    public void consume() {
        try {
            System.out.println("消费"+queue.take());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    
}

 

以上是关于多线程_多生产者多消费者的实现问题的主要内容,如果未能解决你的问题,请参考以下文章

多线程生产者消费者模式中,如何停止消费者?多生产者情况下对“毒丸”策略的应用。

多生产者-多消费者问题

JAVA-初步认识-第十四章-线程间通信-多生产者多消费者问题-JDK1.5新特性解决办法-范例

ParallelProgramming-多消费者,多生产者同时运行并行

多线程的多消费者和多生产者signal和await

Linux多进程通信多生产者一消费者