packagehello;publicclassMain{publicstaticvoidmain(String[]args){newNewThread("First");newNewThread("Second");newNewThread("Third");try{Thread.sleep(10000);}catch(Interrup...
package hello;
public class Main
{
public static void main(String[] args)
{
new NewThread("First");
new NewThread("Second");
new NewThread("Third");
try
{
Thread.sleep(10000);
}
catch(InterruptedException e)
{
System.out.println("Mainthread Interrupted");
}
System.out.println("Main thread exiting");
}
}
class NewThread implements Runnable
{
String name;
Thread t;
NewThread(String th)
{
name =th;
t=new Thread(this,name);
System.out.println("New thread:"+t);
t.start();
}
public void run()
{
try
{
for(int i=3;i>=0;i--)
{
System.out.println(name+":"+i);
Thread.sleep(1000);
}
}catch(InterruptedException e)
{
System.out.println(name+"Interrupted");
}
System.out.println(name+"exiting)");
}
}
展开