CPU consumption when changing threads when using Thread.sleep


I have a server program that polls the database for new queries, I want this poll to run at 1 minute intervals, so I set Thread.sleep () in the program during the loop.
The problem is that whenever this program should "sleep", CPU consumption increases sharply (namely, about 25-30%).
Paradoxically, when a program is not at rest and processing requests is busy, processor consumption drops to 0.4%.
I read online and found out that there are performance problems related to thread.sleep, but I could not find any viable alternative (Thread.wait requires an object notification, which in my opinion is useless in my script)

The main loop (when there are no new requests) does nothing, here is the skeleton of everything that is done when the processor consumption is 25%

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

+5
source share
2 answers

Check what processor consumes for individual processor cores. If you use a 4-core machine, maybe one thread goes rogue and goes once (25%). This usually happens when the stream is in a narrow loop.

Thread.wait ( Timer), , . Thread.sleep, Thread.wait not runnable. JVM .., CPU . , .

, - , , . kill -3 Linux ctrl + break java, Windows. , . , - .

+6

, Thread.sleep .
, , .
, , . , "" - , . , .
, , .

+3

All Articles