wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Thread.sleep() not working as expected Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Thread.sleep() not working as expected" Watch "Thread.sleep() not working as expected" New topic
Author

Thread.sleep() not working as expected

Eric Daly
Ranch Hand

Joined: Jul 11, 2006
Posts: 143
Hi, I am studying for SCJP using Kathy Sierra and Bert Bates SCJP 5 study guide. However, I figured this would be the best place to ask this question; it seems simple enough. In the threads chapter, it mentions that Thread's sleep() method takes a time in milliseconds as an argument. There are 1000 milliseconds in one second, so if I pass 1000 to the Thread.sleep() method, it should sleep for *at least* one second. I know there are no guarantees, but for some reason on my system, it is consistently "asleep" for 10 seconds. I have this code:

There is a nice, precise 10 second pause between each print statement. When I use sleep(100) there is a nice 1 second pause each time. Am I missing something???


Studying for SCJP 6
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
The sleep is indeed only sleeping for 1 second each iteration, however you're only executing a print statement every 10 iterations, therefore you are correctly observing 10 seconds between prints.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Eric Daly
Ranch Hand

Joined: Jul 11, 2006
Posts: 143
Oooooohhhhh. Haha, I better get on top of these things if I plan on passing the SCJP test. It's like all this studying is making me dumber. Thanks for the help.
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 686
If you'd add a few println() statements to the code you'd see what it was doing.
Eric Daly
Ranch Hand

Joined: Jul 11, 2006
Posts: 143
Yea I know... I should have spent a couple minutes trying to actually figure it out... I was too busy trying to understand how Threads work though, and this was just a thought in the back of my mind that I wanted to ask about.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread.sleep() not working as expected