Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

Thread.sleep takes lot of CPU time!

Thread view: 
Raga - 17 Jul 2006 14:49 GMT
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.
Philipp Leitner - 17 Jul 2006 15:05 GMT
> Hi,
>
[quoted text clipped - 25 lines]
> 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
Raga - 18 Jul 2006 07:10 GMT
Hi Philipp,

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

Regards,
Raga

> > Hi,
> >
[quoted text clipped - 48 lines]
>
> /philipp
pranshu - 17 Jul 2006 17:02 GMT
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
Raga - 18 Jul 2006 07:09 GMT
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

> Hi Raga,
> How are you measuring CPU time ?
[quoted text clipped - 5 lines]
>
> Pranshu


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.