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);
            }
        }
    }
相关推荐
-银雾鸢尾-14 小时前
C#中的反射关键类-Type
开发语言·c#
淡海水16 小时前
15-07-YooAsset面试篇-Unity性能优化与内存管理
java·unity·面试·性能优化·c#·游戏引擎
hez20101 天前
.NET 11 Runtime Async 详解
c#·.net·.net core
猿长大人1 天前
C# | Serilog 新手入门
开发语言·c#·.net·log
在世修行1 天前
从零打造 C# 工业视觉检测系统(七):串口通信 SerialPort 封装与开发
开发语言·c#·modbus rtu·rs232/rs485
NoteStream1 天前
【C语言基础】分支和循环(上)
c语言·开发语言·c++·经验分享·笔记·算法·c#
lzhdim2 天前
C# 通过 Windows API 实现进程内存读写操作
开发语言·windows·c#
-银雾鸢尾-2 天前
C#中的预处理指令
开发语言·c#
dalong102 天前
Savitzky-Golay 的C# 实现
开发语言·kotlin·c#
猿长大人2 天前
C# | Autofac 新手指南:从零理解依赖注入与组件装配
c#·.net·di·依赖注入