zoukankan      html  css  js  c++  java
  • SmartThreadPool

    首先是实例化的时候的参数的解释

    //Initialize SmartThreadPool & Make logs
    //SmartThreadPool m_hThreadPool;
    //m_hThreadPool = new SmartThreadPool();//声明一个线程池
    STPStartInfo stp = new STPStartInfo();//线程详细配置参数
    //m_hThreadPool.STPStartInfo这个属性是只读属性,所以只能在实例化的时候设置
    {
        stp.AsReadOnly();//返回一个只读类型的STPStartInfo
        //一个枚举值,储存工作项执行完成后是否调用回调方法,
        //Never不调用,
        //WhenWorkItemCanceled只有当工作项目被取消时调用
        //WhenWorkItemNotCanceled只有当工作项目不取消调用
        //Always调用
        stp.CallToPostExecute = CallToPostExecute.Always;//在这里选择总是回调
        //当工作项执行完成后,是否释放工作项的参数,如果释放,参数对象必须实现IDisposable接口
        stp.DisposeOfStateObjects = true;
        //当线程池中没有工作项时,闲置的线程等待时间,超过这个时间后,会释放掉这个闲置的线程,默认为60秒
        stp.IdleTimeout = 300;//300s
        //最大线程数,默认为25,
        //注意,由于windows的机制,所以一般最大线程最大设置成25,
        //如果设置成0的话,那么线程池将停止运行
        stp.MaxWorkerThreads = 15;//15 thread
        //只在STP执行Action<...>与Func<...>两种任务时有效
        //在执行工作项的过程中,是否把参数传递到WorkItem中去,用做IWorkItemResult接口取State时使用,
        //如果设置为false那么IWorkItemResult.State是取不到值的
        //如果设置为true可以取到传入参数的数组
        stp.FillStateWithArgs = true;
        //最小线程数,默认为0,当没有工作项时,线程池最多剩余的线程数
        stp.MinWorkerThreads = 5;//5 thread
        //当工作项执行完毕后,默认的回调方法
        stp.PostExecuteWorkItemCallback = delegate(IWorkItemResult wir) { MessageBox.Show("ok" + wir.Result); };
        //是否需要等待start方法后再执行工作项,?默认为true,当true状态时,STP必须执行Start方法,才会为线程分配工作项
        stp.StartSuspended = true;
    }
    m_hThreadPool = new SmartThreadPool(stp);//带线程初始化的线程池初始化

    以下是使用两种方法定义函数运行等待返回结果的演示,一种是等待实例化中的对象全部执行完成,一种是等待其中的某些执行完成。

    private void button1_Click(object sender, EventArgs e)
    {
       
        //这个例子将演示传入数个参数并且等待运行然后传出的全过程
        SmartThreadPool stp = new SmartThreadPool();
        IWorkItemResult<string> resultCallback = stp.QueueWorkItem(new Amib.Threading.Func<string, string, string>(GetResultstring), "hello ", "world");
        stp.Start();
        stp.WaitForIdle();//等待该实例下的所有结果返回
        MessageBox.Show(resultCallback.Result);
        stp.Shutdown();
    }
    
    private string GetResultstring(string str, string str2)
    {
        return str + str2;
    }
    private void button2_Click(object sender, EventArgs e)
    {
        //这个例子将演示一批参数的传入一批线程并且等待执行结束返回值
        SmartThreadPool stp = new SmartThreadPool();
        List<IWorkItemResult> t_lResultItem = new List<IWorkItemResult>();//不对IWorkItemResult定义其类型,其结果需要自己做类型转换
        for (int step = 0; step != 100;step++ )
        {
            //这里使用另一种方法来做函数
            t_lResultItem.Add(stp.QueueWorkItem(new WorkItemCallback(GetObjectString), new string[] { "hello ", step.ToString() }));
        }
        stp.Start();
        //等待所需的结果返回
        if (SmartThreadPool.WaitAll(t_lResultItem.ToArray()))
        {
            foreach (IWorkItemResult t in t_lResultItem)
            {
                MakeLog(string.Format("{0}{1}", t.State, t.Result));
            }
        }
    }
    
    private object GetObjectString(object obj)
    {
        return string.Format("{0}{1}", (obj as string[])[0], (obj as string[])[1]);
    }

    处理线程执行过程中出现的错误

    private void button3_Click(object sender, EventArgs es)
    {
        //处理线程执行过程中出现的错误
        SmartThreadPool stp = new SmartThreadPool();//如果需要将线程池设置为调用start的时候才运行,需要设置其StartSuspended参数为true,然后为其调用start方法来启动
        IWorkItemResult<double> ret = stp.QueueWorkItem(new Amib.Threading.Func<double, double, double>(Diverse), 10.0, 0);
        //接收错误的句柄
        stp.Start();
        Exception e = null;
        double resule = ret.GetResult(out e);//在取出结果的时候判断是否有错误产生
        if (e != null)
        {
            //在这里进行错误处理,错误在InnerException中
            MessageBox.Show(e.InnerException.Message);
        }
        else
        {
            MessageBox.Show(resule.ToString());
        }
        stp.Shutdown();
    }
    
    private double Diverse(double x, double y)
    {
        return x/y;
    }

    使用线程分组

    private void button4_Click(object sender, EventArgs e)
    {
        //这里演示了线程的分组
        SmartThreadPool stp = new SmartThreadPool();
        //创建一个分组并用这个分组管理
        IWorkItemsGroup mainGroup = stp.CreateWorkItemsGroup(1);//如果需要设置这个分组为调用start的时候才开始运行,需要传入WIGStartInfo参数,将其参数中的StartSuspended设置为true然后调用分组的start方法
        //向分组中添加任务->当然可以有返回值
        mainGroup.QueueWorkItem(new WorkItemCallback(GetObjectString), 123);
        //分组等待所有任务完成
        mainGroup.WaitForIdle();
        //关闭
        stp.Shutdown();
    }
    SmartThreadPool smartThreadPool = new SmartThreadPool();
    
    //获取当前线程池中的工作线程数,与InUseThreads可能会有差别,因为InUseThreads不包含Idle状态的线程
    int threadNum = smartThreadPool.ActiveThreads;
    //取消所有工作项,如果工作项在执行,那么等待工作项执行完
    smartThreadPool.Cancel();
    //如果不想等待工作项执行完,
    smartThreadPool.Cancel(true);
    //线程池的最大并发数,即MaxWorkerThreads,
    //如果修改后的Concurrency小于MinWorkerThreads,那么MinWorkerThreads也会随之改变
    smartThreadPool.Concurrency = 25;
    //创建一个工作组,最大并发为3,工作组在后面会详细说明,
    smartThreadPool.CreateWorkItemsGroup(3);
    //卸载线程池
    smartThreadPool.Dispose();
    //反回所有未执行的工作项的参数对象
    smartThreadPool.GetStates();
    //获取线程池中正在工作的线程数,与ActiveThreads会有差别,因为ActiveThreads可能包含Idle状态的线程
    int useThreadNum = smartThreadPool.InUseThreads;
    //当线程池用没有工作项时,反回true,否则,反回false
    bool IsIdle = smartThreadPool.IsIdle;
    //同时并行执行多个方法,并且阻塞到所有工作项都执行完,这里会有多少个工作项就会创造多少个线程,
    smartThreadPool.Join(new Action[] { new Action(Test) });
    //获取或设置最大线程数,即MaxWorkerThreads,
    smartThreadPool.MaxThreads = 25;
    //最小线程数,当没有工作项时,线程池最多剩余的线程数
    smartThreadPool.MinThreads = 0;
    //线程池的名称,没什么特殊的用处,
    smartThreadPool.Name = "StartThreadPool";
    //当线程池中没有工作项(即闲置)时触发的事件
    smartThreadPool.OnIdle += new WorkItemsGroupIdleHandler(smartThreadPool_OnIdle);
    //当线程池启动一个线程时,触发的事件
    smartThreadPool.OnThreadInitialization += new ThreadInitializationHandler(smartThreadPool_OnThreadInitialization);
    //当线程池释放一个线程时,所触发的事件
    smartThreadPool.OnThreadTermination += new ThreadTerminationHandler(smartThreadPool_OnThreadTermination);
    //与Join方法类似,并行执行多个带参数的方法,这里会有多少个工作项就会创造多少个线程
    smartThreadPool.Pipe<object>(new object(), new Action<object>[] { new Action<object>(Test) });
    //卸载线程池
    smartThreadPool.Shutdown();
    //启动线程池
    smartThreadPool.Start();
    //STPStartInfo对象的只读实例
    STPStartInfo stpStartInfo = smartThreadPool.STPStartInfo;
    //等待所有的工作项执行完成(即IsIdle为true)
    smartThreadPool.WaitForIdle();
    //获取还未执行的工作项数量
    int wiNum = smartThreadPool.WaitingCallbacks;
    //WorkItemGroup的启动信息的只读实力
    WIGStartInfo wigStartInfo = smartThreadPool.WIGStartInfo;
    ****************
    
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    // 创建一个线程池
    SmartThreadPool smartThreadPool = new SmartThreadPool();
     
     // 执行任务
    smartThreadPool.QueueWorkItem(() =>
    {
         Console.WriteLine("Hello World!");
    });
    带返回值的任务:
    
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    // 创建一个线程池
    SmartThreadPool smartThreadPool = new SmartThreadPool();
     
    // 执行任务
    var result = smartThreadPool.QueueWorkItem(() =>
    {
        var sum = 0;
        for (var i = 0; i < 10; i++)
            sum += i;
     
        return sum;
    });
     
    // 输出计算结果
    Console.WriteLine(result.Result);
    等待多个任务执行完成:
    
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    // 创建一个线程池
    SmartThreadPool smartThreadPool = new SmartThreadPool();
     
    // 执行任务
    var result1 = smartThreadPool.QueueWorkItem(() =>
    {
        //模拟计算较长时间
        Thread.Sleep(5000);
     
        return 3;
    });
     
    var result2 = smartThreadPool.QueueWorkItem(() =>
    {
        //模拟计算较长时间
        Thread.Sleep(3000);
     
        return 5;
    });
     
    bool success = SmartThreadPool.WaitAll(
        new IWorkItemResult[] { result1, result2 });
     
    if (success)
    {
        // 输出结果
        Console.WriteLine(result1.Result);
        Console.WriteLine(result2.Result);
    }
    5、结论 使用SmartThreadPool可以简单就实现支持多线程的程序,由线程池来管理线程,可以减少死锁的出现。SmartThreadPool还支持简单的生产者-消费者模式,当不需要对任务进行持久化时,还是很好用的。
    
    6、扩展阅读 http://www.codeproject.com/KB/threads/smartthreadpool.aspx
    
    http://smartthreadpool.codeplex.com/
    
    http://www.albahari.com/threading/
  • 相关阅读:
    2016/3/10 Java 错题
    2016/3/9 Java 错题集
    Java Socket 编程实验总结
    CSU 1290
    CSU 1307
    CSU 1060
    Problem B SPOJ DCEPC11I
    activemq 学习系列(二) 常用配置
    activemq 学习系列(一) 详细入门使用
    MySql 用户创建与授权
  • 原文地址:https://www.cnblogs.com/wangchuang/p/4526614.html
Copyright © 2011-2022 走看看