Results 1 to 2 of 2
  1. #1
    kendel is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Default 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.

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    12

    Default

    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

  1. Producer Consumer Synchronization Problem
    By rushhour in forum Threads and Synchronization
    Replies: 3
    Last Post: 11-23-2010, 07:44 PM
  2. Towers of Hanoi using Producer/Consumer Pattern
    By dooey in forum New To Java
    Replies: 0
    Last Post: 09-08-2009, 12:45 PM
  3. One Producer - Many Consumers - Same Message
    By zhackwyatt in forum Threads and Synchronization
    Replies: 1
    Last Post: 04-23-2008, 08:27 PM
  4. Replies: 0
    Last Post: 04-09-2008, 06:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •