• 浅谈ThreadPool 线程池(引用)


    出自:http://www.cnblogs.com/xugang/archive/2010/04/20/1716042.html

    浅谈ThreadPool 线程池


    相关概念:

        线程池可以看做容纳线程的容器;

        一个应用程序最多只能有一个线程池;

        ThreadPool静态类通过QueueUserWorkItem()方法将工作函数排入线程池;

        每排入一个工作函数,就相当于请求创建一个线程;

     

    线程池的作用:

    线程池是为突然大量爆发的线程设计的,通过有限的几个固定线程为大量的操作服务,减少了创建和销毁线程所需的时间,从而提高效率。

    如果一个线程的时间非常长,就没必要用线程池了(不是不能作长时间操作,而是不宜。),况且我们还不能控制线程池中线程的开始、挂起、和中止。

     

    什么时候使用ThreadPool


     

    ThreadPool 示例一 :

    复制代码
    ThreadPool_1.cs
    using System;
    using System.Text;
    using System.Threading;

    namespace 多线程
    {
        
    public class Example
        {
            
    public static void Main()
            {
                
    // Queue the task.
                ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));

                Console.WriteLine(
    "Main thread does some work, then sleeps.");
              
                Thread.Sleep(
    1000);

                Console.WriteLine(
    "Main thread exits.");
            }

            
    static void ThreadProc(Object stateInfo)
            {
                
    // No state object was passed to QueueUserWorkItem, 
                
    // so stateInfo is null.
                Console.WriteLine("Hello from the thread pool.");
            }
        }
    }
    复制代码


    ThreadPool 示例 :

    复制代码
    ThreadPool_2.cs
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;

    namespace CS_Test
    {
        
    class ThreadPool_Demo
        {
            
    // 用于保存每个线程的计算结果
            static int[] result = new int[10];


            
    //注意:由于WaitCallback委托的声明带有参数,
            
    //      所以将被调用的Fun方法必须带有参数,即:Fun(object obj)。
            static void Fun(object obj)
            {
                
    int n = (int)obj;

                
    //计算阶乘
                int fac = 1;
                
    for (int i = 1; i <= n; i++)
                {
                    fac 
    *= i;
                }
                
    //保存结果
                result[n] = fac;
            }

            
    static void Main(string[] args)
            {
                
    //向线程池中排入9个工作线程
                for (int i = 1; i <= 9 ; i++)
                {
                    
    //QueueUserWorkItem()方法:将工作任务排入线程池。
                    ThreadPool.QueueUserWorkItem(new WaitCallback(Fun),i);
                    
    // Fun 表示要执行的方法(与WaitCallback委托的声明必须一致)。
                    
    // i   为传递给Fun方法的参数(obj将接受)。
                }

                
    //输出计算结果
                for (int i = 1; i <= 9; i++)
                {
                    Console.WriteLine(
    "线程{0}: {0}! = {1}",i,result[i]);
                }
            }

        }
    }
    复制代码


     

    ThreadPool的作用:

  • 相关阅读:
    设计模式 --单例模式
    Neor Profile SQL 中文汉化
    office online server 安装部署整合到到C#项目
    C# 线程池
    WinForm版 屏幕截图
    golang-nsq高性能消息队列
    【Go Time】Go语言里的条件语句else、switch
    【Go Time】Go语言常量定义关键字const
    【Go Time】Go定义变量
    【Go Time】Go语言里的空接口
  • 原文地址:https://www.cnblogs.com/netcorner/p/3722214.html
Copyright © 2020-2023  润新知