Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CPU consumption when thread is sleeping using Thread.sleep


I have a server program which polls a database for new requests , I want this polling to be done at 1 minute intervals so , I've set up a Thread.sleep() in the program while loop.
The problem is that whenever this program is supposed to "sleep" the CPU consumption goes up drastically (viz. about 25 - 30%).
Paradoxically, when the program is not dormant and is busy processing requests , the CPU consumption drops to 0.4%.
I read online and found out that there are performance hits associated with thread.sleep, but I could not find any viable alternative (Thread.wait requires notification on an object, something which I feel is useless in my scenario)

The main loop (when there are no new requests) doesn't do anything, here is a skeleton of all that is being done when the CPU consumption is 25%

-> poll
-> No new records ?
-> Sleep
->repeat

like image 421
angryInsomniac Avatar asked Oct 14 '11 07:10

angryInsomniac


People also ask

Does sleep waste CPU cycles?

1 Answer. Show activity on this post. In Linux sleep doesn't consume CPU cycles, at least not more than not sleeping would.

What happens when thread sleeps?

Thread. sleep causes the current thread to suspend execution for a specified period. This is an efficient means of making processor time available to the other threads of an application or other applications that might be running on a computer system.

Why should we not use thread sleep?

The reason people discourage Thread. sleep is because it's frequently used in an ill attempt to fix a race condition, used where notification based synchronization is a much better choice etc. In this case, AFAIK you don't have an option but poll because the API doesn't provide you with notifications.

Do blocked threads consume CPU?

A thread is inactive when in the blocked or waiting state. When in these states, the thread does not consume any CPU cycles. A thread is in the waiting state when it wants to wait on a signal from another thread before proceeding. Once this signal is received, it becomes runnable.


2 Answers

Check what the CPU consumption is for individual CPU cores. If you are using a 4 core machine, maybe one thread is going rogue and is eating up once core (25%). This usually happens when the thread is in a tight loop.

You could use Thread.wait with a timeout (which indeed the Timer class does), but my bet is that it won't make any difference. Both Thread.sleep and Thread.wait changes the threads' state to not runnable. Although it depends on your JVM implementation etc., the thread shouldn't consume that much CPU in such situation. So my bet is that there is some bug at work.

Another thing you can do is taking a thread dump and see what the thread is doing when this happens. Use kill -3 on a Linux box, or use ctrl+break on the java console window if you are using Windows. Then, examine the thread dump that is dumped to the standard output. Then you can be sure if the thread was actually sleeping or was doing something else.

like image 136
Enno Shioji Avatar answered Sep 21 '22 23:09

Enno Shioji


As many people pointed out, Thread.sleep should and actually does help with dropping the CPU usage drastically.
I omitted certain facts from my original question as I thought they were not relevant.
The main thread was the producer, there was another thread running asynchronously which was the consumer. It turns out that the "sleep" on this thread was inside some weird condition that wasn't getting triggered properly. So the loop on that thread was never sleeping.
Once the sleep thing was eliminated I went ahead and analyzed it closely to realize the problem.

like image 28
angryInsomniac Avatar answered Sep 21 '22 23:09

angryInsomniac