用Java实现生产者消费者
这是家庭作业的生产者消费者模式的实现。 以下实现有什么问题。 我已经google了各种实现,但我无法理解我的错误。
我有一个共享队列
我在同一个锁上同步生产者和消费者
履行
共享队列:
class SharedQueue{ public static Queue queue = new LinkedList(); }
制片人主题:
//The producer thread class Producer implements Runnable{ public void run() { synchronized (SharedQueue.queue) { if(SharedQueue.queue.size() >=5) { try { SharedQueue.queue.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } Random r = new Random(); int x = r.nextInt(10); System.out.println("Inside Producer" + x); SharedQueue.queue.offer(x); SharedQueue.queue.notify(); } } }
消费者线程:
class Consumer implements Runnable{ public void run() { synchronized (SharedQueue.queue) { if(SharedQueue.queue.size() == 0) { try { SharedQueue.queue.wait(); } catch (InterruptedException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } int k = SharedQueue.queue.remove(); System.out.println("Inside consumer" + k); } } }
主程序
public class ProducerConsumerTest { public static void main(String[] args) { Thread p = new Thread(new Producer()); Thread q = new Thread(new Consumer()); p.start(); q.start(); } }
尝试更换:
if(SharedQueue.queue.size() >= 5)
有:
while(SharedQueue.queue.size() >= 5)
和这个:
if(SharedQueue.queue.size() == 0)
有:
while(SharedQueue.queue.size() == 0)
只需在调用notify()
后重新检查条件。
我假设你希望这是一个无休止的生产者消费者循环。 在Eng.Fouad更改之上,将两个同步块替换为:
while (true)
并在消费者中添加通知
int k = SharedQueue.queue.remove(); // make the producer active again SharedQueue.queue.notify(); System.out.println("Inside consumer " + k);
实现Producer Consumer Problem的简便方法是使用信号量。
public class Semaphore { int value; public Semaphore(int intialValue) { this.value = intialValue; } public synchronized void p() { while (value <= 0) { try { this.wait(); } catch (InterruptedException e) { } } value = value - 1; } public synchronized void v() { value = value + 1; this.notify(); } } public class ProducerConsumerUsingSemaphore { private static final int SIZE = 10; public static void main(String[] args) { Semaphore full = new Semaphore(0); Semaphore empty = new Semaphore(SIZE); Semaphore mutex = new Semaphore(1); Vector sQueue = new Vector (); Thread producerThread = new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 5000; i++) { empty.p(); mutex.p(); System.out.println(Thread.currentThread().getName() + " is trying to insert item " + i); sQueue.add(i); mutex.v(); full.v(); } } }); Thread consumerThread = new Thread(new Runnable() { @Override public void run() { while (true) { full.p(); mutex.p(); System.out.println(Thread.currentThread().getName() + " consuming item " + sQueue.remove(0)); mutex.v(); empty.v(); } } }); producerThread.setName("Producer"); consumerThread.setName("Consumer"); consumerThread.start(); producerThread.start(); } }
您可以使用ConcurrentLinkedQueue来管理Producer和Consumer的共享队列。 您可以使用ConcurrentHashMap> collection,它将帮助Producer同时生成,并且Consumer可以同时使用并将Producer中生成的密钥保存在另一个集合对象中,Consumer可以从ConcurrentHashMap>中找到它的密钥并使用它。