Print 1 to 100 using thread class - Java Program

Q. Write a JAVA program to design a screen with two textboxes and start button. Clicking on start should start two threads printing 1 to 100 in two textboxes.

Answer:

In this example, we design a frame with two text boxes and two buttons. On clicking the start button both threads will be started and print 1 to 100 in both textboxes.

ThreadClass.class

import java.awt.*;
import java.awt.event.*;
class  ThreadClass extends Frame implements ActionListener,Runnable
{
     Button b1, b2;
     TextField txt1, txt2;
     int cnt;
     Thread t1 = new Thread(this, "txt1");
     Thread t2 = new Thread(this, "txt12");
     public ThreadClass()
     {
          setLayout(null);
          txt1 = new TextField();
          txt2 = new TextField();
          b1 = new Button("Start");
          b2 = new Button("Stop");
          txt1.setBounds(50,50,100,100);
          txt2.setBounds(160,50,100,100);
          b1.setBounds(50,170,100,30);
          b2.setBounds(160,170,100,30);
          add(txt1);
          add(txt2);
          b1.addActionListener(this);
          b2.addActionListener(this);
          add(b1);
          add(b2);
          setSize(400,400);
          setVisible(true);
          cnt=0;
          addWindowListener(new WindowAdapter()
          {
               public void windowClosing(WindowEvent e)
               {
                    System.exit(0);
               }
          });
     }
     public void actionPerformed(ActionEvent ae)
     {
          String str;
          str=ae.getActionCommand();
          if (str.equals("Start"))
          {
               t1.start();
               t2.start();
          }
          else if (str.equals("Stop"))
          {
               t1.stop();
               t2.stop();
          }
     }
     public void run()
     {
          try
          {
               for (int i=1; i<=100;i++)
               {
                    txt1.setText(""+i);
                    t1.sleep(150);
                    txt2.setText(""+i);
                    t2.sleep(150);
               }
          }
          catch (Exception ex)
          {
               ex.printStackTrace();
          }
     }
     public static void main(String[] args)
     {
          new ThreadClass().show();
     }
}


Output:

number in textbox