Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Control CPU usage at different levels Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Control CPU usage at different levels" Watch "Control CPU usage at different levels" New topic
Author

Control CPU usage at different levels

Siamac Nikoo
Greenhorn

Joined: Mar 19, 2005
Posts: 1
Hello

Using Java, I have spent a lot of time trying to make the sleep(long milli, int nano) method to work. My main purpose is to try and control the CPU usage at different percentage levels.

I got this information from the link below under Threads, �A new form of the sleep() method is provided which allows for sleep times smaller than one millisecond.�

Link is:
http://java.sun.com/j2se/1.5.0/docs/relnotes/features.html#threads

Is it the same sleep method as above? I tried using sleep( 0, 1 ) with Windows XP Pro where I am expecting a CPU usage at or near 100% but its going only at 2%. When I change to sleep( 0 , 0 ) the CPU usage is at 100%.

I am using JBuilder Enterprise and Foundation with JDK 1.5.

If its not possible to make the sleep method work properly, is there another way to do it in Java?

Thanks for your help.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
this is certainly not a beginner's question. It really belongs in the specialised multithreading forum...

Your understanding of how multithreading works seems to be fundamentally flawed.
You cannot control the running of a thread, and certainly not to the precision allowed in theory by the nanosecond precision option.

While in theory it COULD give you nanosecond precision over the minimum time the thread will sleep (NEVER over the exact time) in reality computers cannot (yet) handle such precision and will round to the nearest several milliseconds (most will use a minimum block of 20 msecs or so). As 1 nanosecond rounded to the nearest millisecond (let alone 20 of them) is 0 your thread will be sent to sleep for a period of no time at all.


42
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Control CPU usage at different levels