import java.awt.*; import java.awt.event.*;/**A demonstration of a threaded application with aresponsive interface. Clicking on the "Start" buttonstarts the counter. The button is then labelled "Stop",pressing it will stop the count, and pressing it againwill resume it (etc., etc.). The counting is done byan object in the inner class CounterRunner. */public class CounterWithThread extends Frame{    private TextField textWindow;    private int count;    private Button toggleButton;    private boolean stop;    public static final int HEIGHT = 100;    public static final int WIDTH = 200;    public static void main (String [] args)    {        CounterWithThread counterWindow = new CounterWithThread();        counterWindow.setVisible(true);    }//main    public CounterWithThread ()    {        stop = true;        count = 0;        setSize(WIDTH, HEIGHT);        addWindowListener(new WindowDestroyer());        setTitle("Threaded version");        setBackground(Color.blue);        setLayout(new FlowLayout());        textWindow = new TextField(15);        add(textWindow);        textWindow.setText("Starting....");        toggleButton = new Button("Start");        toggleButton.addActionListener(new ToggleButtonListener());        add(toggleButton);                //Declare, create, and start the counter thread.        CounterRunner counterThread = new CounterRunner();        counterThread.start();    }//CounterWithThread        /*    This inner class does the counting. When an object in    this class is started (using the Thread method start()),    the run() method is automatically invoked. This goes    into an infinite loop that is concurrent with the    listener object for the button. In this way, actionPerformed()    can be invoked "at the same time" that run() is executing.     */    private class CounterRunner extends Thread    {        public void run ()        {            while (true)            {                try {                    sleep(50);                } catch(InterruptedException e) {                    System.err.println("Interrupted.");                }                if (!stop)                    textWindow.setText(Integer.toString(count++));            }        }//run    }//CounterRunner    /*     * This inner class is the private listener class for the     * toggleButton.     */    private class ToggleButtonListener implements ActionListener    {        public void actionPerformed(ActionEvent e)         {            if (e.getActionCommand().equals("Stop"))            {                toggleButton.setLabel("Start");                stop = true;            }            else if (e.getActionCommand().equals("Start"))            {                toggleButton.setLabel("Stop");                stop = false;            }        }//actionPerformed     }}//CounterWithThread