java 线程使用 Thread.sleep 休眠时的 CPU 消耗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7764497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CPU consumption when thread is sleeping using Thread.sleep
提问by angryInsomniac
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)
我有一个服务器程序,它轮询数据库以获取新请求,我希望以 1 分钟的间隔进行轮询,因此,我在程序中设置了一个 Thread.sleep() while 循环。
问题是,每当这个程序应该“休眠”时,CPU 消耗都会急剧上升(即大约 25 - 30%)。
矛盾的是,当程序不休眠并且忙于处理请求时,CPU 消耗下降到 0.4%。
我在网上阅读并发现与 thread.sleep 相关的性能下降,但我找不到任何可行的替代方案(Thread.wait 需要对对象进行通知,我觉得这在我的场景中是无用的)
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%
主循环(当没有新请求时)不做任何事情,这里是 CPU 消耗为 25% 时正在做的所有事情的骨架
-> poll
-> No new records ?
-> Sleep
->repeat
-> 投票
-> 没有新记录?
-> 睡眠
-> 重复
采纳答案by Enno Shioji
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.
检查单个 CPU 内核的 CPU 消耗量。如果您使用的是 4 核机器,那么一个线程可能会流氓并且正在吃掉一个核心 (25%)。这通常发生在线程处于紧密循环中时。
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.
您可以使用Thread.wait
超时(Timer
课程确实如此),但我敢打赌它不会有任何区别。无论Thread.sleep
和Thread.wait
改变线程的状态not runnable
。尽管这取决于您的 JVM 实现等,但在这种情况下线程不应该消耗那么多 CPU。所以我敢打赌,工作中存在一些错误。
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.
您可以做的另一件事是进行线程转储并查看发生这种情况时线程正在做什么。使用kill -3
在Linux机器上,或者如果您使用的是Windows,Java控制台窗口中使用CTRL + BREAK。然后,检查转储到标准输出的线程转储。然后你就可以确定线程是真的在睡觉还是在做其他事情。
回答by angryInsomniac
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.
正如许多人指出的那样,Thread.sleep 应该并且实际上确实有助于大幅降低 CPU 使用率。
我从最初的问题中省略了某些事实,因为我认为它们不相关。
主线程是生产者,还有另一个异步运行的线程是消费者。事实证明,此线程上的“睡眠”处于某种未正确触发的奇怪状态中。所以该线程上的循环从未休眠。
一旦睡眠的东西被消除,我继续仔细分析它以意识到问题。