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

一、线程池的作用

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

线程池的使用范围:

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

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

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

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

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

二、常用方法介绍

1.ThreadPool.QueueUserWorkItem

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

cs 复制代码
 ThreadPool.QueueUserWorkItem(WaitCallback(DoWork), object)

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

cs 复制代码
public delegate void WaitCallback(object state);

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

cs 复制代码
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是线程池中最小线程数和最大线程数

cs 复制代码
// 参数:
        // 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用法

cs 复制代码
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:

cs 复制代码
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 name="ramnum">保存随机数</param>
        /// <param name="mre">线程状态</param>
        public count(int ramnum, ManualResetEvent mre)
        {
            ramNum = ramnum;
            threadSta = mre;
        }
 
        /// <summary>
        /// 线程
        /// </summary>
        /// <param name="threadParam"></param>
        public 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 name="ramNum">传过来的数:产生的随机数</param>
        /// <returns>返回相加的结果</returns>
        public 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博客

相关推荐
hsling松子4 小时前
使用PaddleHub智能生成,献上浓情国庆福
人工智能·算法·机器学习·语言模型·paddlepaddle
dengqingrui1234 小时前
【树形DP】AT_dp_p Independent Set 题解
c++·学习·算法·深度优先·图论·dp
C++忠实粉丝4 小时前
前缀和(8)_矩阵区域和
数据结构·c++·线性代数·算法·矩阵
我的心永远是冰冰哒5 小时前
ad.concat()学习
学习
ZZZ_O^O5 小时前
二分查找算法——寻找旋转排序数组中的最小值&点名
数据结构·c++·学习·算法·二叉树
CV-King5 小时前
opencv实战项目(三十):使用傅里叶变换进行图像边缘检测
人工智能·opencv·算法·计算机视觉
代码雕刻家6 小时前
数据结构-3.9.栈在递归中的应用
c语言·数据结构·算法
雨中rain6 小时前
算法 | 位运算(哈希思想)
算法
slomay6 小时前
关于对比学习(简单整理
经验分享·深度学习·学习·机器学习
IT良6 小时前
c#增删改查 (数据操作的基础)
开发语言·c#