import java.io.*;
import javax.swing.JOptionPane;
class NewThread implements Runnable
{
String name;
Thread t;
NewThread(String threadname)
{
name=threadname;
t=new Thread(this,name);
JOptionPane.showMessageDialog(null,"new thread:"+t);
t.start();
}
public void run()
{
try
{
for(int i=5;i>0;i--)
{
JOptionPane.showMessageDialog(null,name+":"+i);
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
JOptionPane.showMessageDialog(null,name+" interrupted");
}
JOptionPane.showMessageDialog(null,name+"exiting");
}
}
class multithread
{
public static void main(String[] args)
{
new NewThread("one");
new NewThread("two");
new NewThread("three");
try
{
Thread.sleep(10000);
}
catch(InterruptedException e)
{
JOptionPane.showMessageDialog(null,"main thread interrupted");
}
JOptionPane.showMessageDialog(null,"main thread exiting");
}
}
No comments:
Post a Comment