线程池

线程池


static void Main(string[] args)
{
    for (int i = 0; i < 10; i++)
    {
        ThreadPool.QueueUserWorkItem(Download);
    }
    Thrad.Sleep(500);
}

static void Download(Object state)
{
    for (int i = 0; i < 3; i++)
    {
        Console.WriteLine("Downloading...: " + Thread.CurrentThread.ManageThreadId);
        Thread.Sleep(100);
    }
}
  • 使用线程池启动的线程默认是后台线程
  • 如果进程的所有前台线程都结束了,所有的后台线程就会停止,不能把入池的线程改为前台线程。
  • 不能给入池的线程设置优先级或名称
  • 入池的线程只能用于时间较短的任务,如果线程要一直运行,就应该使用 Thread​ 类创建一个线程。

posted @ 2023-10-04 15:53  天空之城00  阅读(17)  评论(0)    收藏  举报