C#__线程池的简单介绍和使用

cs 复制代码
    /*
    线程池原理:(有备无患的默认备用后台线程)
    特点:线程提前建好在线程池;只能用于运行时间较短的线程。
     */
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 10; i++)
            {
                ThreadPool.QueueUserWorkItem(Download); // 将Download函数放入线程池,并调用
                Thread.Sleep(1000);
                Console.WriteLine(i);
            }
        }

        static void Download(Object state) // 使用ThreadPool.QueueUserWorkItem,线程需要有一个Object state参数
        {
            for(int i = 0; i < 3; i++)
            {
                Console.WriteLine("Downloading......" + Thread.CurrentThread.ManagedThreadId); // ManagedThreadId 一个整数,表示此托管线程的唯一标识符。
                Thread.Sleep(1000);
            }
        }
    }
相关推荐
鱼听禅3 小时前
C# 学习笔记-使用 类型化客户端搭建HTTP客户端服务
学习·http·c#·工厂方法模式
geovindu5 小时前
CSharp:Condition Variable Pattern
后端·设计模式·c#·.net·.netcore·条件变量模式·同步型模式
淡海水5 小时前
11-03-Unity-List-T-和Dictionary-TKey-TValue-的性能调优实战
算法·unity·c#·list·dictionary
fogota6 小时前
C# 如何调用系统图标
c#·.net
△曉風殘月〆6 小时前
.NET是什么?为什么要选择.NET?
c#·.net·.net core·clr
fogota6 小时前
C# WPF 按钮加载字体图标 / 动物头像
c#·.net·wpf
fogota6 小时前
C# WPF 按钮加载系统图标 / 动物头像
c#·.net·wpf
军训猫猫头7 小时前
C# 封装 MySQL 自动写入与查询工具类(基于 MySqlConnector)
mysql·c#·.net·wpf
zxy284722530117 小时前
C# 音频倍速播放
c#·音频·naudio·soundtouch·倍速
Water_Sunzhipeng21 小时前
HandyControl Demo 设置特定框架记录
c#