Main Application

/**
 * Sample small Java application.
 *
 * Demonstrates creating a single window (JFrame) and putting
 * a simple GUI in the window.
 * 
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Application extends JFrame
{
	Vector threads = new Vector();
	Vector numbers = new Vector();
	Container c;
	JTextArea output = new JTextArea();
	JTextArea output2 = new JTextArea();

	public Application() {
		super("Array Lab");		//sets the title bar of the window
		c = getContentPane();
	}

	public static void main(String [] args) {
		Application app = new Application();

		// set the window size
		app.setSize(500, 300);

		// The following creates an "anonymous" class
		// to add as the window listener.
		app.addWindowListener( new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});

		app.guiInit();
		app.show();
	}

	private void guiInit() {
		JPanel bottom = new JPanel();
		bottom.setLayout( new FlowLayout( FlowLayout.RIGHT ) );

		JButton exit = new JButton("Exit");
		JButton task = new JButton("Do It 1");
		JButton task2 = new JButton("Do It 2");
		JButton task3 = new JButton("Numbers");
		JButton task4 = new JButton("Stop Numbers");

		// Create an 'anonymous' class for the action listener
		task4.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				doSomething4();
			}
		});
		bottom.add( task4 );

		// Create an 'anonymous' class for the action listener
		task3.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				doSomething3();
			}
		});
		bottom.add( task3 );

		// Create an 'anonymous' class for the action listener
		task.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				doSomething();
			}
		});
		bottom.add( task );

		// Create an 'anonymous' class for the action listener
		task2.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				doSomething2();
			}
		});
		bottom.add( task2 );

		// Create an 'anonymous' class for the action listener
		exit.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		bottom.add( exit );

		JPanel textPanel = new JPanel();
		textPanel.setLayout( new GridLayout(1, 2, 5, 5) );
		JScrollPane scroller = new JScrollPane(output);
		JScrollPane scroller2 = new JScrollPane(output2);
		textPanel.add(scroller);
		textPanel.add(scroller2);

		c.add( textPanel, BorderLayout.CENTER );
		c.add( bottom, BorderLayout.SOUTH );
	}

	private void doSomething() {
		CountThread counter = new CountThread(1000, output);
		counter.start();
		threads.add(counter);
	}

	private void doSomething2() {
		CountRunnable counter = new CountRunnable(1000, output2);
		Thread t = new Thread(counter);
		t.start();
		threads.add(t);
	}

	private CreateNumbers create;
	private EatNumbers eat;

	private void doSomething3() {
		create = new CreateNumbers(numbers, output);
		eat = new EatNumbers(numbers, output2);

		create.start();
		eat.start();
	}

	private void doSomething4() {
		create.stopNumbers();
		eat.stopEating();
	}
}

CounterThread

import javax.swing.*;

public class CountThread extends Thread {

	private int howHigh;
	private JTextArea output;

	public CountThread(int max, JTextArea out) {
		howHigh = max;
		output = out;
	}

	public void run() {
		for (int i = 0; i <= howHigh; i++) {
			output.setText( Integer.toString(i) );
			try {
				Thread.sleep(10);
			}
			catch(InterruptedException e) {
			}
		}
	}
}

CounterRunnable

import javax.swing.*;

public class CountRunnable implements Runnable
{

	private int howHigh;
	private JTextArea output;

	public CountRunnable(int max, JTextArea out) {
		howHigh = max;
		output = out;
	}

	public void run() {
		for (int i = 0; i <= howHigh; i++) {
			output.setText( Integer.toString(i) );
			try {
				Thread.sleep(10);
			}
			catch(InterruptedException e) {
			}
		}
	}
}

CreateNumber Thread

import java.util.*;
import javax.swing.*;

public class CreateNumbers extends Thread {

	private Vector store;
	private JTextArea output;
	private boolean keepRunning = true;

	public CreateNumbers(Vector s, JTextArea out) {
		store = s;
		output = out;
	}

	public void stopNumbers() {
		keepRunning = false;
	}

	public void run() {
		while(keepRunning) {
			int number = (int)(Math.random() * 100) + 1;
			store.add( new Integer(number) );
			output.append( Integer.toString(number) + "\n" );
			try {
				Thread.sleep(number);
			}
			catch(InterruptedException e) { }
		}
	}
}

EatNumber Thread


import java.util.*;
import javax.swing.*;

public class EatNumbers extends Thread {

	Vector store;
	JTextArea output;
	boolean eatNumbers = true;

	public EatNumbers(Vector s, JTextArea out) {
		store = s;
		output = out;
	}

	public void stopEating() {
		eatNumbers = false;
	}

	public void run() {
		while(eatNumbers) {	
			if (store.size() > 0) {
				Integer i = (Integer)store.remove(0);	// get the first item
				output.append( i.toString() + "\n" );
			}
			else {
				output.append("no numbers in vector\n");
			}
			try {
				Thread.sleep(20);
			}
			catch(InterruptedException e) { }
		}
	}
}