• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling Thread.run after Thread.start !

 
Gagan Indus
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Consider the following code plz :

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

------------------
Gagan (/^_^\)
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The solution is quite simple. Let's look again at that bit of code in java.lang.Thread you quoted:

Given that Ttest11.run() is not being called when you expect it to, the issue must be that target has been set to null. A little rummage in the code leads me to the following:

Evidently, as soon as a thread has finished executing, the target field gets set to null and thread.run() no longer invokes the target's run() method. Other than what you suspected, run() is still called as after the thread has started, but it is no longer called as soon as the thread has finished. This seems to fit well with observed output.
I would agree that Sun should've updated the javadoc for Thread.run().
- Peter
 
Gagan Indus
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankx Peter !
wow , yes it was indeed simple as u said . Never bothered to check da private exit() method , tho i was suspecting tht target!=null comparision is being failed somehow .
I experimented wid the code with this-new point in mind , and the output seen to second the code-functionality expected . After Thread t finishes , target is made null ( Aggressively null-ing a object , as api-doc says ) , n so call to t's run do NOT leads to a implicit call to test.run().
Thankx once again !
Happy Coding !
------------------
Gagan (/^_^\)
 
Andrew Trumper
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why didn't you just do test.run()?
 
rajashree ghatak
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gagan,
Thanx for ur post to my query.Read this post but i want to know where is the run() impleamention of Thread class given which u quoted?i have checked Thread class in APIs for this purpose but in vain.
[Q]
public void run() {
if (target != null) {
target.run();
}
}
[/Q]
Also is the reason mentioned by Peter den Haan as to why
t.run() doesn't implicitly call test.run() after t.start() is executed
also the reason why we cannot call start() method on the same thread twice?Once the first t.start() is executed target is set to null and hence the secondt.start() doesn't yeild anything.
rajashree.

 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic