塘厦镇网站仿做,网络系统运维,常用分类信息网站,微信引流推广一、线程池的作用 线程池是一种多线程处理形式#xff0c;处理过程中将任务添加到队列#xff0c;然后在创建线程后自动启动这些任务。线程池线程都是后台线程。每个线程都使用默认堆栈大小#xff0c;以默认的优先级运行#xff0c;并处于多线程单元中。如果某个线程在托管…一、线程池的作用 线程池是一种多线程处理形式处理过程中将任务添加到队列然后在创建线程后自动启动这些任务。线程池线程都是后台线程。每个线程都使用默认堆栈大小以默认的优先级运行并处于多线程单元中。如果某个线程在托管代码中空闲如正在等待某个事件则线程池将插入另一个辅助线程来使所有处理器保持繁忙。如果所有线程池线程都始终保持繁忙但队列中包含挂起的工作则线程池将在一段时间之后创建另一个辅助线程。但线程的数目永远不会超过最大值。超过最大值的其他线程可以排队但它们要等到其他线程完成后才启动。
线程池的使用范围
1不需要前台执行的线程。
2不需要在使用线程具有特定的优先级。
3线程的执行时间不易过长否则会使线程阻塞。由于线程池具有最大线程数限制因此大量阻塞的线程池线程可能会阻止任务启动。
4不需要将线程放入单线程单元。所有 ThreadPool 线程均不处于多线程单元中。
5不需要具有与线程关联的稳定标识或使某一线程专用于某一任务。
二、常用方法介绍
1.ThreadPool.QueueUserWorkItem 该方法是线程池中最主要的方法ThreadPool.QueueUserWorkItem 方法是用于将工作项提交到线程池队列中的方法。当你需要执行一个方法但不想创建一个新的线程时可以使用该方法。这个方法会将工作项放到一个线程池队列中并由线程池中的一个线程来执行该工作项。 ThreadPool.QueueUserWorkItem(WaitCallback(DoWork), object)
该方法主要是两个参数第一个是WaitCallback第二个是一个objectobject参数可以作为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的参数进行传入然后开启线程。
2.SetMinThreads和SetMaxThreads
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);
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用法
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; //存放线程计算结果/// summary/// 传递数据/// /summary/// param nameramnum保存随机数/param/// param namemre线程状态/parampublic count(int ramnum, ManualResetEvent mre){ramNum ramnum;threadSta mre;}/// summary/// 线程/// /summary/// param namethreadParam/parampublic void ThreadPoolCallback(Object threadParam){int threadIndex (int)threadParam;Console.WriteLine(线程 {0} 启动, threadIndex);total docount(ramNum);Console.WriteLine(线程执行结果 {0}, total);threadSta.Set(); //5、设置每个线程为有信号状态通知WaitOne不再阻塞}/// summary/// 从0开始加到传过来数/// /summary/// param nameramNum传过来的数产生的随机数/param/// returns返回相加的结果/returnspublic int docount(int ramNum){int sum 0;for (int i 0; i ramNum; i){sum i;}return sum;}}
}
参考文献
C#中的线程池使用方法_c# 线程池-CSDN博客
C#多线程--线程池ThreadPool_c# 主线程和 线程池-CSDN博客
C#知识点讲解之ManualResetEvent类的使用-CSDN博客
C#线程池实例多参_c# 线程池多参数-CSDN博客
C#多线程和线程池_c#线程池和线程的区别-CSDN博客