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 :)
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) {
}
}
}
}
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();
}
}
}
}
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]);
}
}
}
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