Write a Java program to design a screen with two textbox and start button. Clicking on start button should start two threads printing 1 to 100 in two textbox.
Write a Java program to design a screen with two textbox and start button. Clicking on start button should start two threads printing 1 to 100 in two textbox.
Program:
import java.io.*; import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.*; public class ThreadBox extends JApplet implements Runnable, ActionListener { TextArea ta1, ta2; Button btnStart; Thread t1, t2; public void init(){ ta1 = new TextArea(1,20); ta2 = new TextArea(1,20); btnStart = new Button("Start"); setLayout(new FlowLayout()); btnStart.addActionListener(this); add(ta1); add(ta2); add(btnStart); } public void run(){ int i; String str = Thread.currentThread().getName(); if(str.equals("First")){ for(i=1;i<=100;i++){ try{ ta1.append(" "+i) t1.sleep(500); }catch(InterruptedException e){ System.out.println(e); } } } else if(str.equals("Second")){ for(i=1;i<=100;i++){ try{ ta2.append(" "+i) t2.sleep(500); }catch(InterruptedException e){ System.out.println(e); } } } } public void actionPerformed(ActionEvent ae){ t1 = new Thread(this); t1.setName("First"); t1.start(); t2 = new Thread(this); t2.setName("Second"); t2.start(); } }
Comments
Post a Comment