简体   繁体   English

Java线程 - cpu用法

[英]Java threads - cpu usage

Here is a quote from a textbook I'm reading at the moment: 以下是我正在阅读的教科书的引用:

"That is, whenever a thread needs to execute a loop with a lot of iterations, it is good practice to put a sleep() in each iteration - Event short sleep times, such as 5 milliseconds, can reduce the overall CPU usage of the application from 100% to > 1%" “也就是说,每当一个线程需要执行一个包含大量迭代的循环时,最好在每次迭代中放入一个sleep() - 事件短暂的休眠时间,例如5毫秒,可以减少整个CPU的使用率。申请率从100%到> 1%“

It's a good practice, I believe, but; 我相信这是一个很好的做法,但是; doesn't scheduler does exactly that - A bit of time to thread1 ; 不是调度程序就是这样 - 对thread1有点时间; suspend thread1 ; 暂停thread1 ; A bit of time to thread2...etc. 有点时间去thread2 ......等等。 I can't grasp such drop rate, anyone willing to enlighten me? 我无法掌握这样的掉落率,有谁愿意开导我?

You see this a lot in programs that update the display of something. 在更新某些内容的程序中,您会看到很多这样的内容。 It's called Busy Waiting, and that's bad. 它被称为忙碌等待,这很糟糕。

If you have a loop that does something like this 如果你有一个类似这样的循环

public void run() {
    while(running) {
        gui.render();
    }
}

You're going to chew up your CPU when you really don't need to. 当你真的不需要时,你会咀嚼你的CPU。 Do you need to render it, over and over and over, 100000+ times a second? 你需要一遍又一遍地渲染它,每秒100000次以上吗? No, you really only need about 30 frames a second. 不,你真的只需要每秒约30帧。

public void run() {
    while(running) {
        gui.render();
        try { Thread.sleep(10); } catch(InterruptedException e) { /* we tried */}
    }
}

This will limit you to a little under 100 frames per second, and you'll end up with a much better performance. 这将限制你每秒不到100帧,你最终会有更好的性能。

You don't always want this for processor intensive background threads, as you want them to have priority. 对于处理器密集型后台线程,您并不总是希望这样,因为您希望它们具有优先级。 That being said, if your background takes all the CPU, how will you process further input (like, I don't know, a CANCEL button because you didn't mean to start that intensive, hours-long calculation?) 话虽这么说,如果你的背景占用了所有的CPU,你将如何处理进一步的输入(比如,我不知道,一个取消按钮,因为你不是故意开始这种密集的,长达数小时的计算?)

So adding a small sleep to your threads CAN BE a very good decision. 因此,为您的线程添加一个小睡眠可能是一个非常好的决定。

The scheduler does just that. 调度程序就是这样做的。
The difference is that the scheduler does it properly. 不同之处在于调度程序正确执行。 Meaning that it will use the CPU efficiently, and that's why you'll get a good CPU usage. 这意味着它将有效地使用CPU,这就是为什么你会获得良好的CPU使用率。

I don't see anything bad about it. 我没有看到任何不好的事情。 It just means it's working. 它只是意味着它的工作。

When you let it sleep there will be more idle time, and you'll reduce CPU usage. 当你让它睡觉时会有更多的空闲时间,你将减少CPU的使用。 If that is you goal for some reason. 如果那是你出于某种原因的目标。
High CPU usage isn't harmful (unless you get over-heating in which case you have a hardware problem). 高CPU使用率是无害的(除非您遇到过热,否则会出现硬件问题)。

Usually when I approach multi-threading problems I actually aim for a high CPU usage. 通常当我处理多线程问题时,我实际上的目标是高CPU使用率。 This usually means that the problem is divided evenly between the threads and I'm getting the maximum from the CPU. 这通常意味着问题在线程之间平均分配,并且我从CPU获得最大值。

If you're using 1% of the CPU it means it's not working, so why have such a good computer ? 如果你使用1%的CPU意味着它不能工作,为什么要有这么好的电脑呢? You should take advantage of the hardware. 你应该利用硬件。

When your program does number-crunching (or other cpu intensive tasks) you want it to run at 100%, don't you? 当你的程序执行数字运算(或其他cpu密集型任务)时,你希望它以100%运行,不是吗?

OTOH, if your program is waiting for input, then you should use asynchronous programming as much as possible and not run endlessly in a loop (asynchronous = system calls you). OTOH,如果你的程序正在等待输入,那么你应该尽可能多地使用异步编程,而不是在循环中无休止地运行(异步=系统调用你)。

Forget it. 算了吧。 What does limiting the CPU usage to 1% buy you? 什么限制CPU使用率为1%买你? Nothing at all? 什么都没有?

Limiting the CPU usage by a factor of 100 means in general slowing down the app by the factor. 将CPU使用率限制为100倍意味着通常会减慢应用程序的速度。

You may want it in case there are other more important threads or in case you may want to stop this thread using Thread.interrupt() , but both can be achieved otherwise. 如果有其他更重要的线程,或者你可能想要使用Thread.interrupt()来阻止这个线程,你可能需要它,但两者都可以实现。

I have never heard of such practise, however it would probably result in huge drop in CPU usage. 我从未听说过这种做法,但它可能会导致CPU使用率大幅下降。 This is because the "bits" of time that scheduler gives for every threads are very small, so that 5ms is considerable amount of time comparing to that. 这是因为调度程序为每个线程提供的“时间”位非常小,因此与此相比,5ms是相当长的时间。

With that said, I see one place you could benefit from such slowing down of your thread: responsiveness on single-core machines. 话虽如此,我看到一个地方,你可以从你的线程放慢速度中受益:单核机器的响应能力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM