Re: Thread.sleep takes lot of CPU time!




Raga wrote:
Hi,

I have the run method like this:

....
....
public void run()
{
...

while(true)
{
...
//some set of operations
....

Thread.sleep(5*1000*60);

}



In the above code, the sleep operation seems to take a lot of CPU time
though ideally sleep is like a no-op mode, meaning since no operation
is done when a thread sleeps, it should not use any/minimal CPU time.
But it seems to take over 30% of CPU time!

Any ideas on why such a weird thing's happening?

In fact, in one more similar method also, sleep is taking similar CPU
time.

Hmmm ... no, on my computer (Mac OS X 10.4, Java 5) I don't have
similar problems:

I wrote something like


public class ThreadTest {
public static void main(String[] args) {
try {
for(;;)
Thread.sleep(5*1000*60);
}
catch(Exception e) {
e.printStackTrace();
}
}
}

to test it, and it does not seem to consume much CPU while sleeping:

902 java 0.0% 0:00.50

/philipp

.



Relevant Pages

  • Re: Thread.sleep takes lot of CPU time!
    ... I checked the performance statistics when doing a stress test. ... the sleep operation seems to take a lot of CPU time ... public class ThreadTest { ...
    (comp.lang.java.programmer)
  • Thread.sleep takes lot of CPU time!
    ... public void run ... the sleep operation seems to take a lot of CPU time ... is done when a thread sleeps, it should not use any/minimal CPU time. ...
    (comp.lang.java.programmer)
  • Thread.sleep takes a lot of CPU time!
    ... public void run ... the sleep operation seems to take a lot of CPU time ... is done when a thread sleeps, it should not use any/minimal CPU time. ...
    (comp.lang.java.help)
  • Re: Thread.sleep takes a lot of CPU time!
    ... public void run ... the sleep operation seems to take a lot of CPU time ... you aught to try it on a G4 mac it's more like 70-80% ...
    (comp.lang.java.help)
  • Re: Thread.sleep takes a lot of CPU time!
    ... public void run ... the sleep operation seems to take a lot of CPU time ... a busy while loop; ...
    (comp.lang.java.help)