Translate

Wednesday 8 January 2014

Create multiple threads in java and assign different sleep() time to each Thread.

//Test.java

class simpleThread extends Thread
{
public simpleThread(String str)
{
super(str);
}
public void run()
{
for(int i=0;i<5;i++)
{
try{
String threadName=getName();
if(threadName.equals("A")){
System.out.println(i +": Thread Name: " + getName());
sleep(1000);
}
else if(threadName.equals("B")){
System.out.println("                    "+i +": Thread Name: " + getName());
sleep(3000);
}
else
{
System.out.println("                                        "+i +": Thread Name: "+ getName());
sleep(5000);
}
}
catch(Exception e){System.out.println(e);}
}
}
}

class Test
{
public static void main(String abc[])
{
System.out.println("-----------------------------------------------------------");
new simpleThread("A").start();
new simpleThread("B").start();
new simpleThread("C").start();
}
}

No comments:

Post a Comment