Results 1 to 2 of 2
Thread: Producer-Consumer Problem
- 02-23-2011, 01:34 PM #1
Member
- Join Date
- Sep 2009
- Posts
- 3
- Rep Power
- 0
Producer-Consumer Problem
I am trying to revamp the producer consumer code below to be similar to this:
Producer produces 3 items (strings: food)
There are 3 Consumers (3 threads) each thread can only pull out 2 of a specific food.
Example: Foods = [Apple, Orange, Banana]
Consumer_1 can take out Apple and Banana only
Consumer_2 can take out Apple and Orange only
Etc.
my code:
Java Code:import java.awt.*; import java.util.*; public class ProducerConsumer { public ProducerConsumer() { Runnable r = new Producer(); Thread chef = new Thread(r); chef. start(); // Starts the function t1 Thread homer_1 = new Thread(new Consumer()); homer_1.start(); Thread homer_2 = new Thread(new Consumer()); homer_2.start(); Thread homer_3 = new Thread(new Consumer()); homer_3.start(); } public static void main(String args[]) { ProducerConsumer Appl = new ProducerConsumer(); } } class Producer implements Runnable // Call for the Runnable function { public void run() // Sets the run function for the program { try { for(int i = 0; i < STEPS; i++) { Thread.sleep(1000); System.out.println(" "); Globals.q.enqueue('A'); Globals.q.enqueue('O'); Globals.q.enqueue('B'); System.out.println("\nThis is in Stock: " + Globals.q); } Globals.b1 = true; } catch (InterruptedException e) {} } private static final int STEPS = 10; } class Consumer implements Runnable // Call for the Runnable function { public void run() // Sets the run function for the program { try { for(int i = 0; i < STEPS; i++) { Thread.sleep(500); System.out.println("\nThis is Before Filling: " + Globals.q); Globals.q.dequeue(); System.out.println("\nThis is After Filling: " + Globals.q); } Globals.b2 = true; } catch (InterruptedException e) {} } private static final int STEPS = 25; } class DisplayQueue implements Runnable { public void run() { try { while (true) { if (Globals.b1 && Globals.b2) { System.out.println("\nListing Stock: ..."); while (!Globals.q.isEmpty()) { System.out.println(Globals.q.getFront()); Globals.q.dequeue(); } System.out.println(); System.exit(0); } else Thread.sleep(50); } } catch (InterruptedException e) {} } } class Globals { static boolean b1 = false; static boolean b2 = false; static Queue q = new Queue(); }Last edited by kendel; 02-23-2011 at 01:44 PM.
- 03-04-2011, 01:09 PM #2
the code try to instantiate the interface queue, which will not work. which collection class do you want to use for storing the fruits?
Last edited by j2me64; 03-04-2011 at 01:40 PM.
Similar Threads
-
Producer Consumer Synchronization Problem
By rushhour in forum Threads and SynchronizationReplies: 3Last Post: 11-23-2010, 07:44 PM -
Towers of Hanoi using Producer/Consumer Pattern
By dooey in forum New To JavaReplies: 0Last Post: 09-08-2009, 12:45 PM -
One Producer - Many Consumers - Same Message
By zhackwyatt in forum Threads and SynchronizationReplies: 1Last Post: 04-23-2008, 08:27 PM -
Implementation of the Producer/Consumer problem in Java
By Java Tip in forum java.langReplies: 0Last Post: 04-09-2008, 06:41 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks