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();
}
}
hvt - 23 Feb 2007 13:04 GMT
On Feb 23, 5:34 pm, divyatiwari...@gmail.com wrote:
> Can we not use the getName() method to schedule the threads??
>
[quoted text clipped - 41 lines]
>
> }
Replace if(s=="squeek") to if(s.equals("squeek")), ur program ll wrk
fine with this chg!
Proton Projects - Moin - 23 Feb 2007 13:11 GMT
On Feb 23, 5:34 pm, divyatiwari...@gmail.com wrote:
> Can we not use the getName() method to schedule the threads??
>
[quoted text clipped - 41 lines]
>
> }
Hi,
if(s=="squeek")
> Thread.sleep(5000);
Need to be changed to
if(s.equals("squeek"))
> Thread.sleep(5000);
Try this u will get a delay....so that quack will get executed first
and then the squeek
Bcoz String comparison need to be done by using equals method...
Check this link for more info
http://access1.sun.com/FAQSets/newtojavatechfaq.html#9
Regards
Moin..
divyatiwari123@gmail.com - 26 Feb 2007 07:36 GMT
Hey Thanks guys. It worked.
divyatiwari123@gmail.com - 26 Feb 2007 07:39 GMT
Hey Thanks guys. It worked. forgot the String comparison part while
writing :) .