zoukankan      html  css  js  c++  java
  • 多线程19-线程池批量插入线程

        class Program
        {
            static void UseThreads(int numberOfOperations)
            {
                using (var countdown = new CountdownEvent(numberOfOperations))
                {
                    Console.WriteLine("Scheduling work by creating threads");
                    for (int i = 0; i < numberOfOperations; i++)
                    {
                        var thread = new Thread(() =>
                        {
                            Console.WriteLine("current Thread Id={0}", Thread.CurrentThread.ManagedThreadId);
                            Thread.Sleep(TimeSpan.FromSeconds(0.1));
                            countdown.Signal();
                        });
                        thread.Start();
                    }
                    countdown.Wait();
                    Console.WriteLine();
                }
            }
            static void UseThreadPool(int numberOfOperations)
            {
                using (var countdown = new CountdownEvent(numberOfOperations))
                {
                    Console.WriteLine("Starting work on a threadPool");
                    for (int i = 0; i < numberOfOperations; i++)
                    {
                        ThreadPool.QueueUserWorkItem(x =>
                         {
                             Console.WriteLine("current Thread Id={0}", Thread.CurrentThread.ManagedThreadId);
                             Thread.Sleep(TimeSpan.FromSeconds(0.1));
                             countdown.Signal();
                         });
                    }
                    countdown.Wait();
                    Console.WriteLine();
                }
            }
            static void Main()
            {
                const int numberOfOpeartions = 3000;
                var sw = new Stopwatch();
                sw.Start();
                UseThreads(numberOfOpeartions);
                sw.Stop();
                Console.WriteLine("Excution time using threads:{0}", sw.ElapsedMilliseconds);

                sw.Reset();
                sw.Start();
                UseThreadPool(numberOfOpeartions);
                sw.Stop();
                Console.WriteLine("Execution time using threads:{0}", sw.ElapsedMilliseconds);
            }
        }
  • 相关阅读:
    xml文件格式例如以下
    Nio学习3——基础模型:多路复用模型
    BP神经网络基本原理
    WPS2012交叉引用技巧,word比wps这点强更新參考文献
    hdu1023
    使用MERGE语句同步表
    商业软件编程非常无聊?
    jQuery Easy UI Panel(面板)组件
    很具体GC学习笔记
    cocos2dx-js学习笔记(一)环境搭建
  • 原文地址:https://www.cnblogs.com/shidengyun/p/5610126.html
Copyright © 2011-2022 走看看