• C#学习系列相关之多线程(五)----线程池ThreadPool用法


    一、线程池的作用

            线程池是一种多线程处理形式,处理过程中将任务添加到队列,然后在创建线程后自动启动这些任务。线程池线程都是后台线程。每个线程都使用默认堆栈大小,以默认的优先级运行,并处于多线程单元中。如果某个线程在托管代码中空闲(如正在等待某个事件),则线程池将插入另一个辅助线程来使所有处理器保持繁忙。如果所有线程池线程都始终保持繁忙,但队列中包含挂起的工作,则线程池将在一段时间之后创建另一个辅助线程。但线程的数目永远不会超过最大值。超过最大值的其他线程可以排队,但它们要等到其他线程完成后才启动。

    线程池的使用范围: 

    (1)不需要前台执行的线程。

    (2)不需要在使用线程具有特定的优先级。

    (3)线程的执行时间不易过长,否则会使线程阻塞。由于线程池具有最大线程数限制,因此大量阻塞的线程池线程可能会阻止任务启动。

    (4)不需要将线程放入单线程单元。所有 ThreadPool 线程均不处于多线程单元中。

    (5)不需要具有与线程关联的稳定标识,或使某一线程专用于某一任务。
     

    二、常用方法介绍

    1.ThreadPool.QueueUserWorkItem

            该方法是线程池中最主要的方法,ThreadPool.QueueUserWorkItem 方法是用于将工作项提交到线程池队列中的方法。当你需要执行一个方法但不想创建一个新的线程时,可以使用该方法。这个方法会将工作项放到一个线程池队列中,并由线程池中的一个线程来执行该工作项。

     ThreadPool.QueueUserWorkItem(WaitCallback(DoWork), object)

    该方法主要是两个参数,第一个是WaitCallback,第二个是一个object,object参数可以作为WaitCallback方法的参数传入。

    public delegate void WaitCallback(object state);

     WaitCallback是一个委托类型,委托参数类型是object定义,因此传入WaitCallback的方法也应当是object类型。

    1. codepublic static void DoWork(object state)
    2. {
    3. // 执行一些操作,使用传递进来的状态对象
    4. }
    5. static void Main(string[] args)
    6. {
    7. // 将 DoWork 方法添加到线程池中
    8. ThreadPool.QueueUserWorkItem(new WaitCallback(DoWork), someStateObject);
    9. }

    someStateObject是DoWork的参数进行传入,然后开启线程。

    2.SetMinThreads和SetMaxThreads

    SetMinThreads和SetMaxThreads是线程池中最小线程数和最大线程数 

    1. // 参数:
    2. // workerThreads:
    3. // 要由线程池根据需要创建的新的最小工作程序线程数。
    4. // completionPortThreads:
    5. // 要由线程池根据需要创建的新的最小空闲异步 I/O 线程数。
    6. // 返回结果:如果更改成功,则为 true;否则为 false。
    7. [SecuritySafeCritical]
    8. public static bool SetMinThreads(int workerThreads, int completionPortThreads);
    9. // 参数:
    10. // workerThreads:
    11. // 线程池中辅助线程的最大数目。
    12. // completionPortThreads:
    13. // 线程池中异步 I/O 线程的最大数目。
    14. // 返回结果:如果更改成功,则为 true;否则为 false。
    15. [SecuritySafeCritical]
    16. public static bool SetMaxThreads(int workerThreads, int completionPortThreads)
    17. 例如:
    18. ThreadPool.SetMinThreads(1,1);
    19. ThreadPool.SetMaxThreads(5, 5);

    3.ManualResetEvent用法

    1.ManualResetEvent 调用一次Set()后将允许恢复所有被阻塞线程。需手动在调用WaitOne()之后调用Reset()重置信号量状态为非终止,然后再次调用WaitOne()的时候才能继续阻塞线程,反之则不阻塞

    2.AutoResetEvent,调用一次Set()只能继续被阻塞的一个线程,多次调用Set()才行,但不需手动调用Reset();再次调用WaitOne()的时候又能阻塞线程,也是和前者的区别

    3.两者单个实例均可阻塞一个或多个线程,在多个线程中调用 主线程 创建的 两者单个实例.WaitOne(),前提是两者实例必须是非终止状态

    4.两者实例化构造参数解释

    public AutoResetEvent(bool initialState);

    true:设置终止状态。相当于调用了Set(),即首次不会被WaitOne()阻塞,下次执行WaitOne()才会被阻塞

    false:设置非终止状态。遇到WaitOne()立即阻塞所在的一个或多个线程

    具体参考一下文章:

    C#学习(二十八)——ManualResetEvent的理解和使用-CSDN博客

    三、ThreadPool代码

    代码1:关于ManualResetEvent用法

    1. using System;
    2. using System.Threading;
    3. public class Example
    4. {
    5. // mre is used to block and release threads manually. It is
    6. // created in the unsignaled state.
    7. private static ManualResetEvent mre = new ManualResetEvent(false);
    8. static void Main()
    9. {
    10. Console.WriteLine("\nStart 3 named threads that block on a ManualResetEvent:\n");
    11. //中文注释1:开启三个线程,每个线程开启后调用WaitOne()阻塞。
    12. for(int i = 0; i <= 2; i++)
    13. {
    14. Thread t = new Thread(ThreadProc);
    15. t.Name = "Thread_" + i;
    16. t.Start();
    17. }
    18. Thread.Sleep(500);
    19. Console.WriteLine("\nWhen all three threads have started, press Enter to call Set()" +
    20. "\nto release all the threads.\n");
    21. Console.ReadLine();
    22. //中文注释2:只有当Set()后才会执行WaitOne()后的代码
    23. mre.Set();
    24. Thread.Sleep(500);
    25. Console.WriteLine("\nWhen a ManualResetEvent is signaled, threads that call WaitOne()" +
    26. "\ndo not block. Press Enter to show this.\n");
    27. Console.ReadLine();
    28. //中文注释3:继续再开两个线程,仍然调用WaitOne(),但是不会阻塞,会继续执行
    29. for(int i = 3; i <= 4; i++)
    30. {
    31. Thread t = new Thread(ThreadProc);
    32. t.Name = "Thread_" + i;
    33. t.Start();
    34. }
    35. Thread.Sleep(500);
    36. Console.WriteLine("\nPress Enter to call Reset(), so that threads once again block" +
    37. "\nwhen they call WaitOne().\n");
    38. Console.ReadLine();
    39. //中文注释4:只有Reset()后,下面再开线程就会继续被阻塞
    40. mre.Reset();
    41. // Start a thread that waits on the ManualResetEvent.
    42. Thread t5 = new Thread(ThreadProc);
    43. t5.Name = "Thread_5";
    44. t5.Start();
    45. Thread.Sleep(500);
    46. Console.WriteLine("\nPress Enter to call Set() and conclude the demo.");
    47. Console.ReadLine();
    48. //中文注释5:再次Set(),就可以了
    49. mre.Set();
    50. // If you run this example in Visual Studio, uncomment the following line:
    51. //Console.ReadLine();
    52. }
    53. private static void ThreadProc()
    54. {
    55. string name = Thread.CurrentThread.Name;
    56. Console.WriteLine(name + " starts and calls mre.WaitOne()");
    57. mre.WaitOne();
    58. Console.WriteLine(name + " ends.");
    59. }
    60. }
    61. /* This example produces output similar to the following:
    62. Start 3 named threads that block on a ManualResetEvent:
    63. Thread_0 starts and calls mre.WaitOne()
    64. Thread_1 starts and calls mre.WaitOne()
    65. Thread_2 starts and calls mre.WaitOne()
    66. When all three threads have started, press Enter to call Set()
    67. to release all the threads.
    68. Thread_2 ends.
    69. Thread_0 ends.
    70. Thread_1 ends.
    71. When a ManualResetEvent is signaled, threads that call WaitOne()
    72. do not block. Press Enter to show this.
    73. Thread_3 starts and calls mre.WaitOne()
    74. Thread_3 ends.
    75. Thread_4 starts and calls mre.WaitOne()
    76. Thread_4 ends.
    77. Press Enter to call Reset(), so that threads once again block
    78. when they call WaitOne().
    79. Thread_5 starts and calls mre.WaitOne()
    80. Press Enter to call Set() and conclude the demo.
    81. Thread_5 ends.

    代码2:

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Diagnostics;
    4. using System.Linq;
    5. using System.Text;
    6. using System.Threading;
    7. using System.Threading.Tasks;
    8. namespace ConsoleApplication1
    9. {
    10. class Program
    11. {
    12. static void Main(string[] args)
    13. {
    14. const int times = 10; //开线程数
    15. ManualResetEvent[] mre = new ManualResetEvent[times]; //1、定义开线程数
    16. Random random = new Random(); //随机数
    17. Console.WriteLine("开始 {0} 任务", times);
    18. for (int i = 0; i < times; i++) //2、循环这10个线程
    19. {
    20. mre[i] = new ManualResetEvent(false); //3、初始化每个线程:设置false表示无信号,将使WaitOne阻塞也就是线程等待
    21. count c = new count(random.Next(1, 1000), mre[i]); //借助类传参
    22. ThreadPool.QueueUserWorkItem(c.ThreadPoolCallback, i); //4、为每个线程安排任务
    23. }
    24. WaitHandle.WaitAll(mre); //6、让主线程等待所有线程完成(池中线程数不能多于64个)
    25. Console.WriteLine("所有线程完成!");
    26. Console.Read();
    27. }
    28. }
    29. class count
    30. {
    31. private int ramNum; //存放随机数
    32. private ManualResetEvent threadSta; //线程状态
    33. private int total; //存放线程计算结果
    34. ///
    35. /// 传递数据
    36. ///
    37. /// 保存随机数
    38. /// 线程状态
    39. public count(int ramnum, ManualResetEvent mre)
    40. {
    41. ramNum = ramnum;
    42. threadSta = mre;
    43. }
    44. ///
    45. /// 线程
    46. ///
    47. ///
    48. public void ThreadPoolCallback(Object threadParam)
    49. {
    50. int threadIndex = (int)threadParam;
    51. Console.WriteLine("线程 {0} 启动", threadIndex);
    52. total = docount(ramNum);
    53. Console.WriteLine("线程执行结果: {0}", total);
    54. threadSta.Set(); //5、设置每个线程为有信号状态:通知WaitOne不再阻塞
    55. }
    56. ///
    57. /// 从0开始加到传过来数
    58. ///
    59. /// 传过来的数:产生的随机数
    60. /// 返回相加的结果
    61. public int docount(int ramNum)
    62. {
    63. int sum = 0;
    64. for (int i = 0; i <= ramNum; i++)
    65. {
    66. sum += i;
    67. }
    68. return sum;
    69. }
    70. }
    71. }

    参考文献:

    C#中的线程池使用方法_c# 线程池-CSDN博客

    C#多线程--线程池(ThreadPool)_c# 主线程和 线程池-CSDN博客

    C#知识点讲解之ManualResetEvent类的使用-CSDN博客

    C#线程池实例(多参)_c# 线程池多参数-CSDN博客

    C#多线程和线程池_c#线程池和线程的区别-CSDN博客

  • 相关阅读:
    springboot 如何更新json串里面的内容
    专升本三本计科仔学习java到实习之路3
    不借助 Javascript,利用 SVG 快速构建马赛克效果
    vue的css取消滑动条,适应在不同的屏幕上
    使用Nginx后,前端无法接收到后端返回的数据
    ThreadLocal夺命11连问
    计算机网络复习02——物理层
    【图像融合】基于matlab增强随机游走算法多焦点图像融合【含Matlab源码 1975期】
    SSM+垃圾分类系统小程序 毕业设计-附源码221511
    Pr 入门系列之十三:添加字幕
  • 原文地址:https://blog.csdn.net/qiaodahua/article/details/133754065