线程非常快地获得 100% CPU
我正在 C# 中实现一个非常基本的线程:
private Thread listenThread;
public void startParser()
{
this.listenThread = new Thread(new ThreadStart(checkingData));
this.listenThread.IsBackground = true;
this.listenThread.Start();
}
private void checkingData()
{
while (true)
{
}
}
然后我立即获得 100% CPU。我想检查传感器数据是否在 while(true) 循环内读取。为什么会这样呢?
提前致谢。
I am implementing a very basic thread in C#:
private Thread listenThread;
public void startParser()
{
this.listenThread = new Thread(new ThreadStart(checkingData));
this.listenThread.IsBackground = true;
this.listenThread.Start();
}
private void checkingData()
{
while (true)
{
}
}
Then I immediately get 100% CPU. I want to check if sensor data is read inside the while(true) loop. Why it is like this?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
while (true)
是杀死 CPU 的原因。您可以添加 Thread.Sleep(X) 来让 CPU 在再次检查之前休息一下。
另外,看来你确实需要一个计时器。
查看此处的 Timer 类之一 http://msdn.microsoft .com/en-us/library/system.threading.timer.aspx。
使用尽可能长的拉动间隔的定时器,1秒,半秒。
您需要在 CPU 使用率和检查之间可以承受的最大延迟之间进行权衡。
while (true)
is what killing your CPU.You can add
Thread.Sleep(X)
to you while to give CPU some rest before checking again.Also, seems like you actually need a Timer.
Look at one of the Timer classes here http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx.
Use Timer with as high pulling interval as you can afford, 1 sec, half a sec.
You need to tradeoff between CPU usage and the maximum delay you can afford between checks.
让你的循环休眠。它跑来跑去,已经累了。至少,让它最终休息一下。
Let your loop sleep. It's running around and around and getting tired. At the very least, let it take a break eventually.
因为你的函数在 while 块内没有做任何事情,所以它会占用 CPU,并且出于所有实际目的,永远不会释放它,因此其他线程可以完成他们的工作
如果将其更改为以下内容,您应该会看到更多合理的CPU消耗:
Because your function isn't doing anything inside the while block, it grabs the CPU, and, for all practical purposes, never lets go of it, so other threads can do their work
If you change it to the following, you should see more reasonable CPU consumption:
您可以使用阻塞队列。从阻塞队列中取出一个项目将阻塞线程,直到有一个项目放入队列中。这不花费任何CPU。
对于 .net4,您可以使用 BlockingCollection http://msdn.microsoft.com/en -us/library/dd267312.aspx
在版本4下,没有阻塞队列int .net框架。
如果你google一下,你可以找到很多阻塞队列的实现。
这是一个实现
http://www.codeproject.com/KB/recipes/boundedblockingqueue.aspx 。
顺便说一句 你等待的数据从哪里来?
请编辑
如果您想检查文件, 。您可以使用 FileSystemWatcher 通过线程块来检查它。
如果您的数据来自外部 API 并且该 api 不会阻塞线程,则除了使用 Thread.Sleep 之外没有其他方法可以阻塞线程
you can use blocking queue. take a item from blocking queue will block the thread until there is a item put into the queue. that doesn't cost any cpu.
with .net4, you can use BlockingCollection http://msdn.microsoft.com/en-us/library/dd267312.aspx
under version 4, there is not blocking queue int .net framework.
you can find many implements of blocking queue if you google it.
here is a implementation
http://www.codeproject.com/KB/recipes/boundedblockingqueue.aspx
by the way. where does the data you wait come from?
EDIT
if you want to check file. you can use FileSystemWatcher to check it with thread block.
if your data comes from external API and the api doesn't block the thread, there is no way to block the thread except use Thread.Sleep
如果你正在调查某种情况,一定要按照其他人的建议去做,然后睡觉。我还想补充一点,如果您需要最大性能,您可以使用统计技巧来避免在读取传感器数据时休眠。当您检测到传感器数据空闲(例如连续 10 次)时,然后在每次迭代时再次开始休眠。
If you're polling for a condition, definitely do as others suggested and put in a sleep. I'd also add that if you need maximum performance, you can use a statistical trick to avoid sleeping when sensor data has been read. When you detect sensor data is idle, say, 10 times in a row, then start to sleep on each iteration again.