import java.awt.*; import java.awt.image.*; import java.awt.event.*; import java.util.*; import java.applet.*; import java.net.*; import java.awt.Component.*; import java.awt.Color.*; import java.awt.Graphics; import java.util.Random; import java.lang.Math; import java.awt.event.WindowListener; // example Thread producer consumer // implemented by Dr. Roger Webster public class producerconsumer extends Applet { Frame window; Button openbutton; Button closebutton; TextArea textarea = new TextArea(24, 80); public Producer b; public Consumer c; public void init() { b = new Producer("The Producer"); b.start(); c = new Consumer("The Consumer", b); c.start(); // openbutton = new Button("KILL Producer"); // closebutton = new Button("KILL Consumer"); // this.add(openbutton); // this.add(closebutton); window = new Frame ("Webster's Sample Java Thread Window"); window.reshape (100, 100, 400, 400); textarea.setFont(new Font("Helvetica", Font.PLAIN, 12)); textarea.setEditable(false); window.add("Center", textarea); textarea.appendText("Java Producer Consumer Lockstep Synchronization Thread example\n"); window.show(); } public void stop() { window.dispose(); b.killme(); c.killme(); } // action - a button pressed. public boolean action(Event event, Object arg) { if (event.target == closebutton) { //window.hide(); c.killme(); } if (event.target == openbutton) { b.killme(); // window.show(); } return true; } // user presses a mouse button. public boolean mouseDown(Event e, int x, int y) { return true; } class Producer extends Thread { long mytime; boolean keeprunning = true; private int mystring = 0; public Producer (String name) { super (name); }//end constructor public void killme() { keeprunning = false; } public synchronized void putmessage() { try { if (mystring == 1) { textarea.appendText("Producer is " + "waiting" +"\n"); System.out.println("Producer is "+ "waiting" +"\n"); wait(); } } catch (Exception e) { System.out.println("exception in thread " + e.toString());} mystring = 1; //new java.util.Date().toString(); textarea.appendText(this.getName()+ " Produced "+ mystring + " \n"); System.out.println(this.getName()+ " Produced "+ mystring +"\n"); notify(); } public synchronized int getmessage() { int s; try { if (mystring == 0) { textarea.appendText("Consumer "+ "waiting" +"\n"); System.out.println("Consumer "+ "waiting" +"\n"); wait(); } } catch (Exception e) { System.out.println("exception in thread " + e.toString());} s = mystring; mystring = 0; textarea.appendText("Consumer consumed: "+s+ " \n"); System.out.println( "Consumer consumed: "+ s +"\n"); notify(); return (s); } public void run() { while (keeprunning) { this.putmessage(); try { mytime = (long)(Math.random() * 10000); Thread.sleep(mytime); } catch (Exception e) { System.out.println("exception in thread sleep" + e.toString());} } }//end run }//end mythread class Consumer extends Thread { long mytime; boolean keeprunning = true; Producer producer; int consumerstring; public Consumer (String name, Producer p) { super(name); producer = p; }//end constructor public void killme() { keeprunning = false; } public void run() { while (keeprunning) { consumerstring = producer.getmessage(); try { mytime = (long)(Math.random() * 1000); Thread.sleep(mytime); } catch (Exception e) { System.out.println("exception in thread sleep" + e.toString());} } }//end run }//end mythread }//end applet