• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Threads

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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'
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Obviously if you want your own Thread instance (td) to sleep just call td.sleep(milis)!
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh yeah!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic