• 普通线程和线程池运行时间对比


      const int intNumber = 500;
            static void Main(string[] args)
            {
                Stopwatch sw = new Stopwatch();
                sw.Start();
                UseThreads(intNumber);
                sw.Stop();
                Console.WriteLine("用线程执行时间:{0}", sw.ElapsedMilliseconds);
                sw.Reset();
                sw.Start();
                UseThreadPool(intNumber);
                sw.Stop();
                Console.WriteLine("用线程执行时间:{0}", sw.ElapsedMilliseconds);
    
            }
    
    
            private static void UseThreadPool(int numberOfOperation)
            {
                using (var countdown = new CountdownEvent(numberOfOperation))
                {
                    Console.WriteLine("开始创建线程:线程池");
                    for (int i = 0; i < numberOfOperation; i++)
                    { 
                        ThreadPool.QueueUserWorkItem(p=>{
                            Console.WriteLine("{0}", Thread.CurrentThread.ManagedThreadId);
                            Thread.Sleep(TimeSpan.FromSeconds(0.1));
                            countdown.Signal();
                        });
                    }
                    countdown.Wait();
                    Console.WriteLine();
                }
            }
    
            private static void UseThreads(int numberOfOperation)
            { 
                using(var countdown = new CountdownEvent(numberOfOperation))
                {
                    Console.WriteLine("开始创建线程:线程");
                    for (int i = 0; i < numberOfOperation; i++)
                    {
                        Thread thread = new Thread(() =>
                        {
                            Console.WriteLine("{0}", Thread.CurrentThread.ManagedThreadId);
                            Thread.Sleep(TimeSpan.FromSeconds(0.1));
                            countdown.Signal();
    
                        });
                        thread.Start();
                    }
                    countdown.Wait();
                    Console.WriteLine();
                    
                }
            }
  • 相关阅读:
    RabbitMQ 部署记录
    Linux下安装db2
    CentOS7 安装 Redis 并设置开机启动
    Firewalld 使用指南
    centos7 redis安装教程
    Centos7下杀毒软件clamav的安装和使用
    centos7 zookeeper集群搭建
    centos开机执行JAR Shell脚本
    centos7 防火墙常用设置
    centos7 Systemd 指令详解
  • 原文地址:https://www.cnblogs.com/sportdog/p/9528231.html
Copyright © 2020-2023  润新知