zoukankan      html  css  js  c++  java
  • ThreadPool

    线程池就是系统管理的,没有执行太耗时的任务建议使用:直接上代码演示:

    class Program
        {
            static void Main(string[] args)
            {
                #region 线程池使用
    
                Console.WriteLine("主线程:" + Thread.CurrentThread.ManagedThreadId);
    
                //线程池的使用:
                ThreadPool.QueueUserWorkItem(obj =>
                {
                    Console.WriteLine("方法一:" + Thread.CurrentThread.ManagedThreadId);
                    //这里的方法就是要进行异步执行的方法
                    for (int i = 0; i < 10; i++)
                    {
                        Console.WriteLine(".");
                        Thread.Sleep(300);
                    }
                });
    
                //线程池的使用:
                ThreadPool.QueueUserWorkItem(new WaitCallback((obj) =>
                {
                    Console.WriteLine("方法二:" + Thread.CurrentThread.ManagedThreadId);
                    //这里的方法就是要进行异步执行的方法
                    for (int i = 0; i < 10; i++)
                    {
                        Console.WriteLine("=");
                        Thread.Sleep(300);
                    }
                }));
    
                //线程池的使用:
                ThreadPool.QueueUserWorkItem(new WaitCallback((obj) =>
                {
                    Console.WriteLine("方法三:" + Thread.CurrentThread.ManagedThreadId);
                    //这里的方法就是要进行异步执行的方法
                    for (int i = 0; i < 10; i++)
                    {
                        Console.WriteLine("*");
                        Thread.Sleep(300);
                    }
                }));
                Console.WriteLine("主线程继续。。。。");
                Console.ReadKey();
                #endregion
    
    
            }
        }
    

      

  • 相关阅读:
    第二章、开发环境部署
    第一章、数据分析介绍
    爬虫之Beautiful Soup
    爬虫之selenium
    使用Python连接Mongodb,对数据库进行操作
    Python练习实例002
    Python练习实例001
    Python入门练手100例
    Python
    剑指Offer-003:从尾到头打印链表
  • 原文地址:https://www.cnblogs.com/entclark/p/8026492.html
Copyright © 2011-2022 走看看