aspose file tools*
The moose likes Beginning Java and the fly likes How to make code stall or sleep Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to make code stall or sleep" Watch "How to make code stall or sleep" New topic
Author

How to make code stall or sleep

Anthony Smith
Ranch Hand

Joined: Sep 10, 2001
Posts: 285
I have a metod that when called the first line of code I would like to write is to have the method stall for 10 miliseconds. How can I do this? This is not a thread.

I would just write a loop to stall, but it would finish at different times depending on what CPU is it is executed on.
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

Even though it is not a separate thread, it still is running within the main thread. You can do:

to get the effect you want.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Tom Billings
Greenhorn

Joined: Sep 12, 2005
Posts: 16
And to continue Joel's reply there's no way to guarantee a 10 ms sleep since it would mean that you're having exclusive control of the processor. Which Java, nor the OS would allow.
Tom Billings
Greenhorn

Joined: Sep 12, 2005
Posts: 16
Sleeping exactly 10 ms, that is.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Anthony Smith:
I have a metod that when called the first line of code I would like to write is to have the method stall for 10 miliseconds. How can I do this? This is not a thread.

I would just write a loop to stall, but it would finish at different times depending on what CPU is it is executed on.


Every program IS a thread. That is every program has at least ONE thread. So you can still use the methods from the Thread class as illustrated above.

Layne


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to make code stall or sleep