C#__Eine kurze Einführung und Verwendung des Thread-Pools

    /*
    线程池原理:(有备无患的默认备用后台线程)
    特点:线程提前建好在线程池;只能用于运行时间较短的线程。
     */
    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);
            }
        }
    }

Supongo que te gusta

Origin blog.csdn.net/qq_57233919/article/details/132754363
Recomendado
Clasificación