线程和CPU使用率100 [英] Threading and CPU usage 100

查看:99
本文介绍了线程和CPU使用率100的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如何使用线程使用无限循环....

当我执行以下代码时...占用100%的CPU使用率...

我如何将CPU使用率至少降低50%

hi ,

how to use the infinite loop using threading....

while i am executing below code... it''s taking 100 % usage of the CPU...

How i can reduce the CPU usage at-least 50 %

namespace ThreadingIssue
{
    class Program
    {
        static void Main(string[] args)
        {
            ThreadStart ts = new ThreadStart(Test);
            Thread th = new Thread(ts);
            th.IsBackground = true;
            th.Priority = ThreadPriority.Lowest;
            th.Start();

            ts = new ThreadStart(Test1);
            th = new Thread(ts);
            th.IsBackground = true;
            th.Priority = ThreadPriority.Lowest;
            th.Start();

            Console.ReadKey();
        }

        static void Test()
        {
            while (true)
            {

            }
        }

        static void Test1()
        {
            while (true)
            {

            }
        }
    }
}

推荐答案

您不只是睡觉,还想在有条件的情况下睡觉.睡眠的答案对您没有帮助.
您永远不要使用旋转等待.您只应等待使用线程同步原语.

另外,最好不要触摸线程优先级.如果操作正确,则不会浪费任何CPU时间.方法如下:

您不需要睡觉,需要等待System.Threading.EventWaitHandle.

线程代码为:

You do not just sleep, you want to sleep on condition. The the answers with sleep won''t help you.
You should never ever use spin wait. You should wait using thread synchronization primitives only.

Also, better not touch thread priority. If you do it right, it will not waste any CPU time. Here is how:

You need not sleep, you need to wait for System.Threading.EventWaitHandle.

Thread code is:

MyEventWaitHandle.WaitOne();



或者,您可以将WaitOne与参数一起使用,这是超时.

在此调用中,操作系统关闭线程,并且从不将其调度回执行,直到将其唤醒.该线程将完全花费零CPU时间.只能从另一个线程调用MyEventWaitHandle.SetThread.Abort来唤醒该线程.就这样.

—SA



Alternatively, you can use WaitOne with a parameter, which is a timeout.

On this call OS switches the thread off and never schedules it back to execution until it is waken up. The thread will spend exactly zero CPU time. The thread can be waken up only be MyEventWaitHandle.Set or Thread.Abort called from the other thread. That''s it.

—SA


100%的CPU并不意味着您的程序无法正常运行,只是意味着它正在使用所有可用的CPU.

除了让其他线程更具响应性之外,没有其他方法可以使您的线程变慢.但是您已经使用ThreadPriority.Lowest做到了.以这种方式更改优先级是使程序尽可能快地运行并使其他线程响应(UI和系统线程)的最佳方法.

使用System.Threading.Thread.Sleep(...)可以使其他线程也能正常工作,但会大大降低您自己的计算速度.

您有可用的CPU:为什么不使用它????只需告诉系统我的任务并不重要"(就像您所做的那样),它将完成其余的工作.

如果需要,您甚至可以更进一步并更改Process.PriorityClass.

------

我阅读了您的评论,并认为您正在寻找的是同步.如果从2个不同的线程访问变量,则必须使用同步对象保护其访问.看看这个链接,例如:
http://www.albahari.com/threading/part2.aspx [
100% CPU doesn''t mean your program is not working properly, it just means it''s using all the available CPU.

There is no sense to make your thread slower except to let the other threads be more responsive. But you already did it with ThreadPriority.Lowest. Changing the priority this way is the best way to make your program run as fast as possible and let the other threads responsive (UI and system threads).

Using System.Threading.Thread.Sleep(...) is good to let other threads work as well, but will slow down your own computations dramatically.

You have available CPU: why wouldn''t you use it??? Just tell the system "my task is not so important" (as you did) and it will do the rest.

You could even go further and change the Process.PriorityClass if you need to.

------

I read your comments and I think what you are looking for is synchronization. If you access a variable from 2 different threads, you must secure its access with synchronization objects. Have a look to this link for example:
http://www.albahari.com/threading/part2.aspx[^]


我同意Dylan,您应该增加某种间隔或睡眠.

您仍然可以随意使用代码,添加10ms等待时间不会减慢计算速度.

希望这会有所帮助.

我使用了AutoResetEvent

问候

特伦斯

Hi , I agree with Dylan, you should add some kind of interval or sleep.

You can still use your code as you please, adding a 10ms wait will not slow down your computations.

Hope this helps.

I used an AutoResetEvent

Regards

Terence

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ThreadingIssue
{
    class Program
    {
        static AutoResetEvent _AREvt;

        static void Main(string[] args)
        {

            _AREvt = new AutoResetEvent(false);

            ThreadStart ts = new ThreadStart(Test);
            Thread th = new Thread(ts);
            th.IsBackground = true;
            th.Priority = ThreadPriority.Lowest;
            th.Start();

            ts = new ThreadStart(Test1);
            th = new Thread(ts);
            th.IsBackground = true;
            th.Priority = ThreadPriority.Lowest;
            th.Start();

            Console.ReadKey();
        }

        static void Test()
        {
            
            while (true)
            {
             
                //do what you need todo
                _AREvt.WaitOne(10, true);

            }
        }

        static void Test1()
        {
            while (true)
            {
             
                //do what you need todo
                _AREvt.WaitOne(10, true);

            }
        }
    }
}


这篇关于线程和CPU使用率100的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆