从visual studio运行多线程程序时,CPU使用率要高得多

时间:2014-12-26 14:35:24

标签: c# multithreading

我觉得这很奇怪。我正在测试一个同时创建1500个线程的程序。当我从visual studio 2013运行这个程序时,cpu使用率会更高(大约60%)但是当我运行exe时,cpu的使用率仍然低于10%。

    static ManualResetEvent _doneEvent = new ManualResetEvent(false);
    private static int _numerOfThreadsNotYetCompleted = 1500;
    private static readonly object ob= new object();
    static void Main(string[] args)
    {
        List<System.Threading.Tasks.Task> taskList = new List<System.Threading.Tasks.Task>();
        Console.Read();
        for (int i = 0; i <= 1500; ++i)
        {
            var j = i;
            var handle = new EventWaitHandle(false, EventResetMode.ManualReset);
            Thread obj = new Thread(() =>
            {
                Thread.Sleep(10000);
                lock (ob)
                {
                    Console.WriteLine(_numerOfThreadsNotYetCompleted);
                    if (Interlocked.Decrement(ref _numerOfThreadsNotYetCompleted) == 0)
                        _doneEvent.Set();
                }       
            });
            obj.Start();
        }
        _doneEvent.WaitOne();
        Console.WriteLine("All done.");
    }

提前致谢

0 个答案:

没有答案