Results 1 to 2 of 2
  1. #1
    concaf is offline Member
    Join Date
    Sep 2011
    Posts
    1
    Rep Power
    0

    Default producer/consumer problem

    following exercise:

    5 producers baking cookies.
    When at least 50 units in stock
    then they inform the 2 consumers.
    Consumers clear the stock.
    When 100 units in stock, production stops
    - use wait()/notify() concept
    - use graphical output (applet/awt/swing)


    okay guys,
    please give me a little help :)

    Java Code:
    package consumer;
    
    import javax.swing.JLabel;
    
    public class Producer extends Thread {
    	JLabel label;
    
    	public Producer(JLabel label) {
    		this.label = label;
    		this.start();
    	}
    
    	public void end() {
    		interrupt();
    	}
    
    	public void run() {
    		while (!isInterrupted()) {
    			try {
    				for (int cookie = 0; cookie <= 100; cookie++) {
    					label.setText(" " + cookie);
    					sleep((int) (Math.random() * 80));
    					if (cookie > 50) {
    						synchronized (this) {
    							try {
    								wait((int) (Math.random() * 100 + 1));
    								System.out.println("cookie-" + this.getName()
    										+ " " + cookie);
    
    							} catch (Exception e) {
    								e.printStackTrace();
    								System.out.println("cookie exception" + cookie);
    							}
    						}
    					}
    					if (cookie == 100) {
    						end();
    						label.setText("RIP");
    					}
    				}
    				break;
    			} catch (Exception e) {
    			}
    		}
    	}
    }
    Java Code:
    package consumer;
    
    import javax.swing.JLabel;
    
    public class Consumer extends Thread {
    	JLabel label;
    
    	public Consumer(JLabel label) {
    		this.label = label;
    		this.start();
    	}
    
    	public void run() {
    		synchronized (this) {
    			try {
    				System.out.println("Producer-"+this.getName()+" started");
    
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    		}
    	}
    
    }
    Java Code:
    package consumer;
    
    import java.awt.BorderLayout;
    
    public class View2 extends JFrame {
    	private JPanel contentPane;
    
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					View2 frame = new View2();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	public View2() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 600, 180);
    		this.setLocationRelativeTo(null);
    		setResizable(false);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		contentPane.setLayout(new BorderLayout(0, 0));
    
    		JPanel panel = new JPanel();
    		contentPane.add(panel, BorderLayout.NORTH);
    
    		JLabel [] producer = new JLabel[3];
    		for(int i = 0; i <3; i++){
    			producer[i]  = new JLabel("Producer "+(i+1));
    			producer[i].setPreferredSize(new Dimension(80, 25));
    			producer[i].setBorder(new MatteBorder(1, 1, 1, 1, Color.black));
    			panel.add(producer[i]);
    		}
    
    		JPanel panel_1 = new JPanel();
    		contentPane.add(panel_1, BorderLayout.CENTER);
    
    		JLabel[] cookie = new JLabel[3];
    		for (int i=0; i<3; i++){
    			cookie[i]= new JLabel();
    			cookie[i].setPreferredSize(new Dimension(40, 25));
    			cookie[i].setBorder(new MatteBorder(1, 1, 1, 1, Color.black));
    			panel_1.add(cookie[i]);
    			new Producer (cookie[i]);
    		}
    
    		JPanel panel_2 = new JPanel();
    		contentPane.add(panel_2, BorderLayout.SOUTH);
    
    		JLabel[] consumer = new JLabel[2];
    		JLabel[] eating = new JLabel[3];
    		for (int i=0; i<2; i++){
    			consumer[i]= new JLabel("Consumer "+(i+1));
    			consumer[i].setPreferredSize(new Dimension(80, 25));
    			consumer[i].setBorder(new MatteBorder(1, 1, 1, 1, Color.black));
    			panel_2.add(consumer[i]);
    
    			eating[i] = new JLabel("hungry");
    			eating[i].setPreferredSize(new Dimension(60, 25));
    			eating[i].setBorder(new MatteBorder(1, 1, 1, 1,Color.black));
    			panel_2.add(eating[i]);
    			new Consumer(eating[i]);
    
    		}
    	}
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default Re: producer/consumer problem

    Read about the ArrayBlockingQueue class in the java.util.concurrent package; it can do all the locking, waiting and notification business for you.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Producer-Consumer Problem
    By kendel in forum Threads and Synchronization
    Replies: 1
    Last Post: 03-04-2011, 01:09 PM
  2. Producer Consumer Synchronization Problem
    By rushhour in forum Threads and Synchronization
    Replies: 3
    Last Post: 11-23-2010, 07:44 PM
  3. Towers of Hanoi using Producer/Consumer Pattern
    By dooey in forum New To Java
    Replies: 0
    Last Post: 09-08-2009, 12:45 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
  •