Thread.sleep takes lot of CPU time!

R

Raga

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.
 
P

Philipp Leitner

Raga said:
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
 
P

pranshu

Hi Raga,
How are you measuring CPU time ?

I guess you are using a profiler.
It is highly likely that your loop is very light - taking 0.01% of
system time - and out of that the sleep method is taking 30%?
essentially being 30% of nothing.


Pranshu
 
R

Raga

Yes, I used profiler.

Its not that way. Out of teh total CPU consumption, sleep() in this
method is taking about 30%, which is very high right? This was observed
when a stress test was done. Has it something to do specifically when
stress testing?

Regards,
Raga
 
R

Raga

Hi Philipp,

I checked the performance statistics when doing a stress test. Maybe,
doing a stress test yields such result??

Regards,
Raga

Philipp said:
Raga said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top