使用ThreadPool创建100%的CPU使用率 [英] Using ThreadPool creates a 100% cpu usage

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

问题描述

我正在使用ThreadPool类对应用程序任务进行排队,但它使用的是100%的cpu使用率。我们可以减少一些吗?



这是代码:



Hi, I am using the ThreadPool class to queue application task, but it is using 100% cpu usage. Can we reduce it some how?

Here is the code:

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

namespace InParallel
{
    class Run
    {

        public static void Main(string[] args)
        {
            try
            {
                while (true) // Loop indefinitely
	            {
	                Console.WriteLine("Enter input:"); // Prompt
	                string line = Console.ReadLine(); // Get string from user
	                if (line == "exit") // Check string
	                {
		            break;
	                }
	                int value;
	                if (int.TryParse(line, out value)) // Try to parse the string as an integer
	                {
	                    switch(value)
                        {
                            case 1:
                                Seqential();
                                break;
                            case 2:
                               UsingThreadPool();
                                break;
                            case 3:
                                UsingParaller();
                                break;
                            case 4:
                                UsingThreadPool2();
                                break;
                        }
	                }
	            }



                GC.Collect();
                Console.ReadLine();
               
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        private static void Seqential()
        {
            Console.WriteLine("*************************Started Seqential *********************************");
            Stopwatch watch = Stopwatch.StartNew();

            for (int i = 2; i < 20; i++)
            {
                var result = SumRootN(i);
                // Console.WriteLine("root {0} : {1} ", i, result);
            }

            //WalkTree(myTree);
            // WalkTreeUsingThreadPool(myTree);
            watch.Stop();
            Console.WriteLine("********************End Seqential***************************************");

            Console.WriteLine("Total Time Taken Sequential " + watch.ElapsedMilliseconds.ToString());
        }
        private static void UsingThreadPool()
        {
             ThreadPool.SetMaxThreads(50,50);
             Console.WriteLine("************************Started UsingThreadPool***************************************");
            Stopwatch watch = Stopwatch.StartNew();
           
            for (int i = 2; i < 20; i++)
            {
                ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc),i);
                Console.WriteLine("donefor" + i);
            }

            //WalkTree(myTree);
            // WalkTreeUsingThreadPool(myTree);
            watch.Stop();
            Console.WriteLine("****************************End UsingThreadPool*****************");

            Console.WriteLine("Total Time Taken UsingThreadPool " + watch.ElapsedMilliseconds.ToString());
        }
        private static void UsingParaller()
        {
            Console.WriteLine("*********************Started UsingParaller********************************");
            Stopwatch watch = Stopwatch.StartNew();

            Parallel.For(2, 20, (i) =>
                {
                    var result = SumRootN(i);
                   // Console.WriteLine("root {0} : {1} ", i, result);
                });
            watch.Stop();
            Console.WriteLine("****************************End UsingParaller******************");

            Console.WriteLine("Total Time Taken UsingParaller " + watch.ElapsedMilliseconds.ToString());
        }
        private static void UsingThreadPool2()
        {
            ThreadPool.SetMaxThreads(50, 50);
            Console.WriteLine("************************Started UsingThreadPool***************************************");
            Stopwatch watch = Stopwatch.StartNew();

            //for (int i = 2; i < 20; i++)
            //{
            //    ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc), i);
            //    Console.WriteLine("donefor" + i);
            //}

            ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc2), 0);
            ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc2), 1);



            //WalkTree(myTree);
            // WalkTreeUsingThreadPool(myTree);
            watch.Stop();
            Console.WriteLine("****************************End UsingThreadPool*****************");

            Console.WriteLine("Total Time Taken UsingThreadPool " + watch.ElapsedMilliseconds.ToString());
        }
       
        static void ThreadProc(Object stateInfo)
        {
            SumRootN((int)stateInfo);
            // No state object was passed to QueueUserWorkItem, so  
            // stateInfo is null.
           Console.WriteLine("Hello from the thread pool."+Thread.CurrentThread.GetHashCode());
        }

        static void ThreadProc2(Object stateInfo)
        {
            int startcount = (int)stateInfo;
            if (startcount == 0)
            {
                for (int i = 0; i < 10; i++)
                {
                    SumRootN(i);
                }
            }
            else 
            {
                for (int i = 10; i < 20; i++)
                {
                    SumRootN(i);
                }
            }
            
            // No state object was passed to QueueUserWorkItem, so  
            // stateInfo is null.
            Console.WriteLine("Hello from the thread pool." + Thread.CurrentThread.GetHashCode());
        }

        
        public static double SumRootN(int root)
        {
            double result = 0;
            for (int i = 1; i < 10000000; i++)
            {
                result += Math.Exp(Math.Log(i) / root);
            }
            Console.WriteLine("root=" + root+"result="+result);
            return result;
        }

    }

   

   

    
}

推荐答案

它与线程池无关,只与您的线程有关。这取决于你在做什么。在许多情况下,你真的想使用100%的CPU,为什么不呢?



正如我所看到的,你正在做一些计算,没有任何同步。这是你真正想要的。毕竟,您想早点完成计算吗?如果你这样做,为什么要浪费一些CPU能力呢? 全部使用!当您开始做其他事情时,例如,在其他进程中使用UI或您自己进程的UI线程时,这些线程将占用CPU负载的份额(根据CPU核心数量和CPU性能,有些延迟可能会对您造成一些影响,但您需要为性能付出一些代价。)



你真的不需要注意使用较少的CPU,你只需要注意浪费CPU时间。可能浪费CPU的一个例子是轮询。为了更好地使用您的系统,您应该尽可能避免轮询,因为它会浪费性能。请参阅:

http://en.wikipedia.org/wiki/Polling_%28computer_science %29 [ ^ ],

http://en.wikipedia.org/wiki/Push_technology [ ^ ](好),

http://en.wikipedia.org/wiki/Pull_technology [ ^ ](游泳池)。



从你的样本中,我看不到这样的东西,所以你可能不用担心



只有在技术上不可避免的轮询或拉动时,你才能考虑减少这种愚蠢线程的CPU负载。一种可能的替代技术可能是将一个CPU核心专用于这样的线程(处理器/代码亲和力 http: //en.wikipedia.org/wiki/Processor_affinity [ ^ ]),使系统的其余部分更具可预测性和稳定性;它可以帮助你拥有足够的CPU /核心。



-SA
It has nothing to do with thread pools, only with your thread(s). It depends on what you are doing. In many cases, you really want to use 100% CPU, why not?

As I can see, you are doing some calculations, without any synchronization with anything. And this is want you really may want. After all, do you want to complete your calculation sooner or not? If you do, why wasting some CPU power? Use it all! When you, say, start to do something else, for example, using UI in other processes or the UI thread of your own process, those threads will take their share of the CPU load (depending on number of CPU cores and CPU performance, some delays might disturb you a bit, but you need to pay some price for performance).

You really don''t need to take care about using less CPU, you only need to take care about wasting of CPU time. One example where you might waste CPU is polling. To use your system well, you really should avoid polling by all means, because it wastes performance. Please see:
http://en.wikipedia.org/wiki/Polling_%28computer_science%29[^],
http://en.wikipedia.org/wiki/Push_technology[^] (good),
http://en.wikipedia.org/wiki/Pull_technology[^] (pool).

From your sample, I cannot see anything like that, so you probably don''t need to worry.

And only if polling or pull is technically unavoidable, you can think about reducing of the CPU load by such a "stupid" thread. One possible alternative technique could be dedicating one of the CPU core to such thread (processor/code affinity, http://en.wikipedia.org/wiki/Processor_affinity[^]), to make the rest of the system more predictable and stable in performance; it can help of you have enough CPUs/cores.

—SA


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

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