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);
            }
        }
    }
相关推荐
wrq14714 小时前
MyAccess 完整使用手册
c#·orm框架
z落落17 小时前
C# WinForm 自定义控件
开发语言·c#
lzhdim19 小时前
20260714 - 个人小作品更新
c#·notenet
小堂子这厢有礼了1 天前
Chet.QuartzNet.UI v2.3.0 大更新!表格重构 + 系统配置 + 分析页全面升级!
前端·后端·ui·重构·c#·vue
asdzx672 天前
使用 Python 精准操控 Word 字体:获取与替换方案
python·c#·word
逝水无殇2 天前
C# 特性详解
开发语言·后端·c#
LongtengGensSupreme2 天前
C#图像内存高速拷贝:使用Marshal.AllocHGlobal与ArrayPool实现内存拷贝的几个方法
开发语言·数码相机·c#
影寂ldy2 天前
C# 五大加密算法全套实战(AES/DES对称、RSA非对称、MD5/SHA256哈希)
开发语言·c#·哈希算法
互联网底层民工2 天前
异步 / 多线程并发(高频八股 · 详解版)
c#
逝水无殇2 天前
C# 反射详解
开发语言·后端·c#