threads scheduling

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();

}

}
 
H

hvt

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();

}

}

Replace if(s=="squeek") to if(s.equals("squeek")), ur program ll wrk
fine with this chg!
 
P

Proton Projects - Moin

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();

}

} 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..
 
D

divyatiwari123

Hey Thanks guys. It worked. forgot the String comparison part while
writing :) .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top