dcsimg
Calling Thread.run() after Thread.start()
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Gagan_Indus
Posted On:   Monday, September 10, 2001 08:46 AM

Consider the following code plz : class Ttest11 implements Runnable { public void run() { for (int i=0;i <6;i++) System.out.print("Hello"+i+" "); } public static void main(String[] r) { Ttest11 test= new Ttest11(); Thread t=new Thread(test); t.start(); for(int i=15;i>0;i--) System.out.print("World"+i+" "); t.run(); // LINE#1 , change it to test.run(); System.out.println("main() Ending"); } } The output of above code as it is : World15 World14 World13 World12 World11 World10 World9 Hello0 Worl   More>>

Consider the following code plz :

			
class Ttest11 implements Runnable {

public void run()
{
for (int i=0;i <6;i++)
System.out.print("Hello"+i+" ");
}

public static void main(String[] r)
{

Ttest11 test= new Ttest11();
Thread t=new Thread(test);

t.start();

for(int i=15;i>0;i--)
System.out.print("World"+i+" ");

t.run(); // LINE#1 , change it to test.run();

System.out.println("main() Ending");
}

}


The output of above code as it is :

World15 World14 World13 World12 World11 World10 World9 Hello0 World8 Hello1 Worl
d7 Hello2 World6 Hello3 World5 Hello4 World4 Hello5 World3 World2 World1 main()
Ending


tht is , 15 times "World" , n 6 times "Hello"

Now Try changing the LINE#1 in above code to "test.run();" , and the output changes to :

World15 World14 World13 World12 World11 World10 World9 World8 World7 World6 Worl
d5 World4 World3 World2 Hello0 World1 Hello1 Hello0 Hello2 Hello1 Hello3 Hello2
Hello4 Hello3 Hello5 Hello4 Hello5 main() Ending


This is 15 times "World" n 12 times "Hello"


So wht exactly is wrong here? t.run() is supposed to call test.run() implicitly , as test was passed as a Runnable to t 's constructor?
( as sugested by run()'s implementation in Thread class :

public void run() {
if (target != null) {
target.run();
}
}
)


I experimented with the code a lot , placing lots of join() , isAlive() , run()s etc , and moving them up n down the code , and a strange observation is " Once the thread t actually gets running , the direct call to t 's run() ( like t.run() ) is simply being ignored ! "
Is any such norm tht we can not call run() explicitly , while the thread in q is actually running ? As far as i knw , NO such norm , becoz other code relating to this give expected outputs
(or are we dealing wid some probable bug of JVM ?! )


DO anyone hav any idea , thought , suggestion about this?
plz help , do comeup with anything u hav in ur mind , on this topic ...    <<Less

Re: Calling Thread.run() after Thread.start()

Posted By:   Simon_Ablett  
Posted On:   Wednesday, September 19, 2001 07:00 AM

You shouldn't be calling the Thread's run() method directly. You should always use start(). The JVM will then invoke run() for you. Note that the time lag between start() being called and the subsequent invocation of run() is implementation dependant. Maybe it is this that's causing the strange behaviour.
Regards.

Re: Calling Thread.run() after Thread.start()

Posted By:   Kevin_Riff  
Posted On:   Saturday, September 15, 2001 06:51 PM

I agree that it's strange. The only thing I can think of is that the source code you get with the JDK is the reference version. The actual production version that you run is usually optimized for a particular platform. Maybe part of that optimization is responsible for the behaviour you're seeing?
About | Sitemap | Contact