File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads" Watch "Threads" New topic
Author

Threads

Mylene Morato
Greenhorn

Joined: Feb 10, 2009
Posts: 10

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?


SCJP 5
Harshit Rastogi
Ranch Hand

Joined: Apr 15, 2008
Posts: 131
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'


<a href="http://technologiquepanorama.wordpress.com" target="_blank" rel="nofollow">My Techie Blog</a><br /><a href="http://www.java-questions.com" target="_blank" rel="nofollow">Java Questions</a>
Sunny Mattas
Ranch Hand

Joined: Apr 22, 2008
Posts: 45
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


Regards
Sunny Mattas
SCJP5
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Obviously if you want your own Thread instance (td) to sleep just call td.sleep(milis)!


be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
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.


All code in my posts, unless a source is explicitly mentioned, is my own.
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
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

Joined: Jul 23, 2008
Posts: 352
Oh yeah!
 
 
subject: Threads