D
divyatiwari123
Can we not use the getName() method to schedule the threads??
Following code gives the output without any delay..............
import java.lang.Thread.*;
class ThreaDemo implements Runnable
{
public void run()
{
try{String s=Thread.currentThread().getName();
if(s=="squeek")
Thread.sleep(5000);
else
{for(int i=0;i<5;i++)
System.out.println(i+" ");
}
System.out.println(Thread.currentThread());
}
catch(Exception e)
{}
}
}
public class runnDemo extends ThreaDemo
{
public static void main(String arg[])
{
ThreaDemo r=new ThreaDemo();
Thread t=new Thread(r);
t.setName("squeek");
t.start();
Thread t1=new Thread(r);
t1.setName("quack");
t1.start();
}
}
Following code gives the output without any delay..............
import java.lang.Thread.*;
class ThreaDemo implements Runnable
{
public void run()
{
try{String s=Thread.currentThread().getName();
if(s=="squeek")
Thread.sleep(5000);
else
{for(int i=0;i<5;i++)
System.out.println(i+" ");
}
System.out.println(Thread.currentThread());
}
catch(Exception e)
{}
}
}
public class runnDemo extends ThreaDemo
{
public static void main(String arg[])
{
ThreaDemo r=new ThreaDemo();
Thread t=new Thread(r);
t.setName("squeek");
t.start();
Thread t1=new Thread(r);
t1.setName("quack");
t1.start();
}
}