线程池是一种多线程处理形式,处理过程中将任务添加到队列,然后在创建线程后自动启动这些任务。线程池线程都是后台线程。每个线程都使用默认堆栈大小,以默认的优先级运行,并处于多线程单元中。如果某个线程在托管代码中空闲(如正在等待某个事件),则线程池将插入另一个辅助线程来使所有处理器保持繁忙。如果所有线程池线程都始终保持繁忙,但队列中包含挂起的工作,则线程池将在一段时间之后创建另一个辅助线程。但线程的数目永远不会超过最大值。超过最大值的其他线程可以排队,但它们要等到其他线程完成后才启动。
线程池的使用范围:
(1)不需要前台执行的线程。
(2)不需要在使用线程具有特定的优先级。
(3)线程的执行时间不易过长,否则会使线程阻塞。由于线程池具有最大线程数限制,因此大量阻塞的线程池线程可能会阻止任务启动。
(4)不需要将线程放入单线程单元。所有 ThreadPool 线程均不处于多线程单元中。
(5)不需要具有与线程关联的稳定标识,或使某一线程专用于某一任务。
该方法是线程池中最主要的方法,ThreadPool.QueueUserWorkItem 方法是用于将工作项提交到线程池队列中的方法。当你需要执行一个方法但不想创建一个新的线程时,可以使用该方法。这个方法会将工作项放到一个线程池队列中,并由线程池中的一个线程来执行该工作项。
ThreadPool.QueueUserWorkItem(WaitCallback(DoWork), object)
该方法主要是两个参数,第一个是WaitCallback,第二个是一个object,object参数可以作为WaitCallback方法的参数传入。
public delegate void WaitCallback(object state);
WaitCallback是一个委托类型,委托参数类型是object定义,因此传入WaitCallback的方法也应当是object类型。
- codepublic static void DoWork(object state)
- {
- // 执行一些操作,使用传递进来的状态对象
- }
-
- static void Main(string[] args)
- {
- // 将 DoWork 方法添加到线程池中
- ThreadPool.QueueUserWorkItem(new WaitCallback(DoWork), someStateObject);
- }
someStateObject是DoWork的参数进行传入,然后开启线程。
SetMinThreads和SetMaxThreads是线程池中最小线程数和最大线程数
-
- // 参数:
- // workerThreads:
- // 要由线程池根据需要创建的新的最小工作程序线程数。
- // completionPortThreads:
- // 要由线程池根据需要创建的新的最小空闲异步 I/O 线程数。
- // 返回结果:如果更改成功,则为 true;否则为 false。
- [SecuritySafeCritical]
- public static bool SetMinThreads(int workerThreads, int completionPortThreads);
- // 参数:
- // workerThreads:
- // 线程池中辅助线程的最大数目。
- // completionPortThreads:
- // 线程池中异步 I/O 线程的最大数目。
- // 返回结果:如果更改成功,则为 true;否则为 false。
- [SecuritySafeCritical]
- public static bool SetMaxThreads(int workerThreads, int completionPortThreads)
- 例如:
- ThreadPool.SetMinThreads(1,1);
- ThreadPool.SetMaxThreads(5, 5);
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博客
代码1:关于ManualResetEvent用法
- using System;
- using System.Threading;
-
- public class Example
- {
- // mre is used to block and release threads manually. It is
- // created in the unsignaled state.
- private static ManualResetEvent mre = new ManualResetEvent(false);
-
- static void Main()
- {
- Console.WriteLine("\nStart 3 named threads that block on a ManualResetEvent:\n");
- //中文注释1:开启三个线程,每个线程开启后调用WaitOne()阻塞。
- for(int i = 0; i <= 2; i++)
- {
- Thread t = new Thread(ThreadProc);
- t.Name = "Thread_" + i;
- t.Start();
- }
-
- Thread.Sleep(500);
- Console.WriteLine("\nWhen all three threads have started, press Enter to call Set()" +
- "\nto release all the threads.\n");
- Console.ReadLine();
- //中文注释2:只有当Set()后才会执行WaitOne()后的代码
- mre.Set();
-
- Thread.Sleep(500);
- Console.WriteLine("\nWhen a ManualResetEvent is signaled, threads that call WaitOne()" +
- "\ndo not block. Press Enter to show this.\n");
- Console.ReadLine();
- //中文注释3:继续再开两个线程,仍然调用WaitOne(),但是不会阻塞,会继续执行
- for(int i = 3; i <= 4; i++)
- {
- Thread t = new Thread(ThreadProc);
- t.Name = "Thread_" + i;
- t.Start();
- }
-
- Thread.Sleep(500);
- Console.WriteLine("\nPress Enter to call Reset(), so that threads once again block" +
- "\nwhen they call WaitOne().\n");
- Console.ReadLine();
- //中文注释4:只有Reset()后,下面再开线程就会继续被阻塞
- mre.Reset();
-
- // Start a thread that waits on the ManualResetEvent.
- Thread t5 = new Thread(ThreadProc);
- t5.Name = "Thread_5";
- t5.Start();
-
- Thread.Sleep(500);
- Console.WriteLine("\nPress Enter to call Set() and conclude the demo.");
- Console.ReadLine();
- //中文注释5:再次Set(),就可以了
- mre.Set();
-
- // If you run this example in Visual Studio, uncomment the following line:
- //Console.ReadLine();
- }
-
- private static void ThreadProc()
- {
- string name = Thread.CurrentThread.Name;
-
- Console.WriteLine(name + " starts and calls mre.WaitOne()");
-
- mre.WaitOne();
-
- Console.WriteLine(name + " ends.");
- }
- }
-
- /* This example produces output similar to the following:
- Start 3 named threads that block on a ManualResetEvent:
- Thread_0 starts and calls mre.WaitOne()
- Thread_1 starts and calls mre.WaitOne()
- Thread_2 starts and calls mre.WaitOne()
- When all three threads have started, press Enter to call Set()
- to release all the threads.
- Thread_2 ends.
- Thread_0 ends.
- Thread_1 ends.
- When a ManualResetEvent is signaled, threads that call WaitOne()
- do not block. Press Enter to show this.
- Thread_3 starts and calls mre.WaitOne()
- Thread_3 ends.
- Thread_4 starts and calls mre.WaitOne()
- Thread_4 ends.
- Press Enter to call Reset(), so that threads once again block
- when they call WaitOne().
- Thread_5 starts and calls mre.WaitOne()
- Press Enter to call Set() and conclude the demo.
- Thread_5 ends.
代码2:
-
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
-
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- const int times = 10; //开线程数
- ManualResetEvent[] mre = new ManualResetEvent[times]; //1、定义开线程数
- Random random = new Random(); //随机数
- Console.WriteLine("开始 {0} 任务", times);
- for (int i = 0; i < times; i++) //2、循环这10个线程
- {
- mre[i] = new ManualResetEvent(false); //3、初始化每个线程:设置false表示无信号,将使WaitOne阻塞也就是线程等待
- count c = new count(random.Next(1, 1000), mre[i]); //借助类传参
- ThreadPool.QueueUserWorkItem(c.ThreadPoolCallback, i); //4、为每个线程安排任务
-
- }
- WaitHandle.WaitAll(mre); //6、让主线程等待所有线程完成(池中线程数不能多于64个)
- Console.WriteLine("所有线程完成!");
- Console.Read();
- }
- }
-
- class count
- {
- private int ramNum; //存放随机数
- private ManualResetEvent threadSta; //线程状态
- private int total; //存放线程计算结果
-
- ///
- /// 传递数据
- ///
- /// 保存随机数
- /// 线程状态
- public count(int ramnum, ManualResetEvent mre)
- {
- ramNum = ramnum;
- threadSta = mre;
- }
-
- ///
- /// 线程
- ///
- ///
- public void ThreadPoolCallback(Object threadParam)
- {
- int threadIndex = (int)threadParam;
- Console.WriteLine("线程 {0} 启动", threadIndex);
- total = docount(ramNum);
- Console.WriteLine("线程执行结果: {0}", total);
- threadSta.Set(); //5、设置每个线程为有信号状态:通知WaitOne不再阻塞
- }
-
- ///
- /// 从0开始加到传过来数
- ///
- /// 传过来的数:产生的随机数
- ///
返回相加的结果 - public int docount(int ramNum)
- {
- int sum = 0;
- for (int i = 0; i <= ramNum; i++)
- {
- sum += i;
- }
- return sum;
- }
- }
- }
-
参考文献:
C#多线程--线程池(ThreadPool)_c# 主线程和 线程池-CSDN博客