Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads

 
Mylene Morato
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello friends,

I have a doubt in this code :




This code prints "ABC"

I would like to know if the output would be "XYZ" as well.

In this code seems that the method Tread.sleep(1000) only is considered for the main thread.
Why not for the td thread?
Because then td thread would sleep, main thread would set the variable to "XYZ" , td would wake up and it would print "XYZ".

Can anybody help me?
 
Harshit Rastogi
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When your run the program ,then a thread called as main thread which execute main method is present. So when you do Thread.sleep() it will make the thread to sleep in which it is running ( which is main thread). So your main thread goes to sleep and thread td prints the output as 'ABC'
 
Sunny Mattas
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this code seems that the method Thread.sleep(1000) only is considered for the main thread.
Why not for the td thread?


Hi

Thread.sleep(long milliseconds) is a static method and can only be called on the current thread means thread which is running.In your code when sleep is called the current thread is main.


Above line will spawn a thread which will get runnable state and eventually the running state.Thread when ever run by JVM will start executing the run method.
If there is a sleep method called in run method or any method called from run method then other thread td will go in sleep state.

Regards
Sunny Mattas
SCJP5
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Obviously if you want your own Thread instance (td) to sleep just call td.sleep(milis)!
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephen Davies wrote:Obviously if you want your own Thread instance (td) to sleep just call td.sleep(milis)!

That's not true, and that's why many people think that that syntax to call a static method is misleading and it lends itself to mistakes like this. If you call td.sleep(milis) inside main() that will only cause main() to sleep, not td. You need to call the static method from the code of the thread you want to sleep, that's the way sleep() works.
 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephen Davies wrote:Obviously if you want your own Thread instance (td) to sleep just call td.sleep(milis)!


No. You have to call Thread.sleep() from thread td (which means from XMap.run()).

sleep() is static method and always causes current thread to sleep.

BTW, why do you have that two useless synchronized blocks in the code?
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh yeah!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic