• 多线程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);
            }
        }
  • 相关阅读:
    5.装饰器生成器迭代器
    python-进程&线程
    模块(二)
    MYSQL数据库操作(一)
    python-网络编程-socket编程
    python-异常处理
    python-面向对象进阶
    python-模块
    python_面向对象编程
    生成器 迭代器,装饰器 ,软件开发规范
  • 原文地址:https://www.cnblogs.com/shidengyun/p/5610126.html
Copyright © 2020-2023  润新知