/* Applet initiating producer and consumer which share a circular buffer. */ import javax.swing.*; public class Buffer extends JFrame { // Height and width of text area private static final int TEXT_HEIGHT = 48; private static final int TEXT_WIDTH = 28; // Max sleep time after producing ints for producer 1 and 2 private static final int SLEEP1 = 200; private static final int SLEEP2 = 100; // Beginning number for producers 1 and 2 private static final int START1 = 10; private static final int START2 = 20; protected JTextArea output; // Output area to write buffer status protected ProduceInteger producer1,producer2; // Thread producing integers protected ConsumeInteger consumer; // Thread consuming integers // Set up output area public Buffer(String title) { super(title); output = new JTextArea(TEXT_WIDTH,TEXT_HEIGHT); getContentPane().add(new JScrollPane(output)); setSize(500,400); } public static void main(String[] args){ Buffer app = new Buffer("Buffer Demo"); app.show(); app.start(); } // Initiate producer and consumer threads public void start() { HoldInteger holder = new HoldInteger(output); // circular buffer producer1 = new ProduceInteger("producer 1",START1,holder,SLEEP1); producer2 = new ProduceInteger("producer 2",START2,holder,SLEEP2); consumer = new ConsumeInteger("consumer",holder); producer1.start(); // Start up producer and consumer threads producer2.start(); consumer.start(); } }