美文网首页.NET多线程
.NET多线程(三)线程池

.NET多线程(三)线程池

作者: 万州大牛 | 来源:发表于2017-01-24 21:46 被阅读0次

    命名空间

    System.Threading.ThreadPool
    

    原因

    线程的创建和销毁是比较昂贵的操作

    对象池的思想

    事先创建好几个对象,需要就从池中分配,用完就返回池中

    注意要点

    (1)线程池,只适合短时操作,不要阻塞线程池线程
    (2)线程池,线程是后台线程
    (3)线程池的线程数量有上限
    (4)ASP.NET 使用自己的线程池

    线程另一种分类(线程池)

    (1)工作线程(worker thread)
    主要处理占用CPU进行算法计算等

    (2)IO线程(I/O thread)
    主要处理网络请求,硬盘文件读写等

    优缺点

    (1)电脑的内存是有限的,每个线程,都会占用内存,如果并发数量很多,内存会爆掉。
    (2)使用线程池,当并发超过一定数量,则会排队,所以,并行的请求处理时间会被延长。

    使用线程池

    (1)使用 QueueUserWorkItem(worker thread)

    Thread thread = new Thread(() => 
    {
        // 打印 False
        Console.WriteLine(Thread.CurrentThread.IsThreadPoolThread);
    });
    thread.Start();
    thread.Join();
    ThreadPool.QueueUserWorkItem((obj) =>
    {
        // 打印 True
        Console.WriteLine(Thread.CurrentThread.IsThreadPoolThread);
    });
    Console.ReadLine();
    

    (2)委托异步调用使用了线程池(worker thread)

    public delegate void HiDelegate();
    static void Main(string[] args)
    {
        HiDelegate sayHi = new HiDelegate(SayHi);
        IAsyncResult ar = sayHi.BeginInvoke((x) => { }, sayHi);
        sayHi.EndInvoke(ar);
        Console.ReadLine();
    }
    static void SayHi()
    {
        // True 使用线程池
        Console.WriteLine(Thread.CurrentThread.IsThreadPoolThread);
    }
    

    (3)System.Threading.Timer 使用了线程池(worker thread)

    System.Threading.Timer timer;
    timer = new Timer((obj) =>
    {
        // True 使用线程池
        Console.WriteLine(Thread.CurrentThread.IsThreadPoolThread);
    });
    timer.Change(500, 500);
    Console.ReadLine();
    

    (4)System.ComponentModel.BackgroundWorker使用了线程池(worker thread)

    System.ComponentModel.BackgroundWorker bgWorker;
    bgWorker = new BackgroundWorker();
    bgWorker.DoWork += (sender, e) =>
    {
        // True 使用线程池
        Console.WriteLine(Thread.CurrentThread.IsThreadPoolThread);
    };
    bgWorker.RunWorkerAsync();
    Console.ReadLine();
    

    标准的取消线程任务

    使用System.Threading.CancellationTokenSource

    using (CancellationTokenSource cts = new CancellationTokenSource())
    {
        CancellationToken token = cts.Token;
        ThreadPool.QueueUserWorkItem((obj) =>
        {
            while (!token.IsCancellationRequested)
            {
                Console.WriteLine("hi");
            }
        });
        Thread.Sleep(3);
        cts.Cancel(); // 取消线程执行
    }
    Console.ReadLine();
    

    以上内容,仅仅代表个人理解,以及参考书资料的观点。
    如果有错误,请及时指出,不要让错误的观点误导更多的人,感谢!

    相关文章

      网友评论

        本文标题:.NET多线程(三)线程池

        本文链接:https://www.haomeiwen.com/subject/chjgbttx.html