import java.awt.*;
import java.awt.event.*;
class NewThread implements Runnable
{
Thread
t;
boolean
flag=true;
public void run()
{
while(flag)
{
System.out.println("Thread is
Running..");
}
}
void
startThread()
{
t=new
Thread(this);
t.start();
}
void
stopThread()
{
flag=false;
}
}
class strtstop extends Frame implements ActionListener
{
Button
b1=new Button("START");
Button
b2 =new Button("STOP");
NewThread t1;
strtstop()
{
setTitle("Implementing Thread start and stop");
setSize(250,200);
setLayout(new FlowLayout());
b1.setVisible(true);
add(b1);
b1.addActionListener(this);
b2.setVisible(true);
add(b2);
b2.addActionListener(this);
this.addWindowListener(new WindowAdapter(){
public
void windowClosing(WindowEvent we)
{
t1.stopThread();
dispose();
}
});
b2.setEnabled(false);
setVisible(true);
}
public
void actionPerformed(ActionEvent ae)
{
try
{
if(ae.getSource()==b1)
{
t1=new NewThread();
t1.startThread();
b1.setEnabled(false);
b2.setEnabled(true);
}
if(ae.getSource()==b2)
{
t1.stopThread();
b1.setEnabled(true);
b2.setEnabled(false);
}
}catch(Exception e)
{}
}
public static
void main(String ar[])
{
strtstop
ss=new strtstop();
}
}
Post a Comment