怎么判断ThreadPool线程池里的任务都执行完毕
c# ThreadPool 判断子线程全部执行完毕的四种方法
1、先来看看这个
多线程编程
多线程用于数据采集时,速度明显很快,下面是基本方法,把那个auto写成采集数据方法即可。
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
ThreadPool.SetMaxThreads(3, 3); //设置最大线程数
for (int i = 0; i < 10; i++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(Auto), i);//线程池指定线程执行Auto方法
}
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public static void Auto(object i)//多线程执行的方法
{
if (string.Equals(i,2))
{
Thread.Sleep(2000);
}
Console.WriteLine(i.ToString());
}
}
}
明白吧,就是多线程执行顺序是不确定的。
2、再来看看这个结果
static void Main(string[] args)
{
try
{
ThreadPool.SetMaxThreads(3, 3); //设置最大线程数
for (int i = 0; i < 10; i++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(Auto), i);//线程池指定线程执行Auto方法
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine("结束了"); //这句要改
Console.ReadLine();
}
public static void Auto(object i)//多线程执行的方法
{
if (string.Equals(i, 2))
{
Thread.Sleep(2000);
}
Console.WriteLine(i.ToString());
}
结束了 这三个字不一定 真正在 最后一行输出。
因为这时是 主线程+子线程 这些线程的执行顺序不确定,可能主线程老早就执行了。
也就说可能结束了 这三个字很早就会输出。
3、主题 保证 结束了 在最后输出。
方法1:
//这是主线程,一直都会执行。
目前一直在进行的是 一个主线程+多个子线程
while (true)
{
Thread.Sleep(1000);//这句写着,主要是没必要循环那么多次。
去掉也可以。
int maxWorkerThreads, workerThreads;
int portThreads;
ThreadPool.GetMaxThreads(out maxWorkerThreads, out portThreads);
ThreadPool.GetAvailableThreads(out workerThreads, out portThreads);
if (maxWorkerThreads - workerThreads == 0)
{
Console.WriteLine("结束了");
break;
}
}
GetAvailableThreads():检索由 GetMaxThreads 返回的线程池线程的最大数目和当前活动数目之间的差值。
而GetMaxThreads 检索可以同时处于活动状态的线程池请求的数目。
通过最大数目减可用数目就可以得到当前活动线程的数目,如果为零,那就说明没有活动线程,说明所有线程运行完毕。
方法2 : Monitor
见下篇文章:/handboy/blog/item/681d093875d6e6cdd56225ae.html
class Program
{
static object locker = new object();
static int runningThreads = 0;
static void Main(string[] args)
{
try
{
ThreadPool.SetMaxThreads(4, 4); //设置最大线程数 using System.Threading;
runningThreads = 10;
for (int i = 0; i < runningThreads; i++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(Auto), i);//线程池指定线程执行Auto方法
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
lock (locker)
{
while (runningThreads > 0)
{
Monitor.Wait(locker);
}
}
Console.WriteLine("结束了");
Console.ReadLine();
}
public static void Auto(object i)//多线程执行的方法
{
if (string.Equals(i, 2))
{
Thread.Sleep(2000);
}
lock (locker)
{
runningThreads--;
Monitor.Pulse(locker);
}
Console.WriteLine(i.ToString());
}
}
方法3:WaitHandle (推荐用这个方法)
。
/ganggang0217/blog/item/fe2a004ecad3acdcd0c86a67.html
public void testThreads()
{
ManualResetEvent[] _ManualEvents = new ManualResetEvent[10];
for (int i = 0; i < 10; i++)
{
_ManualEvents[i] = new ManualResetEvent(false);
System.Threading.ThreadPool.QueueUserWorkItem(new WaitCallback(testMethod), _ManualEvents[i]);
}
WaitHandle.WaitAll(_ManualEvents);
// 线程结束后执行后面的主线程代码
Console.WriteLine("结束了");
Console.ReadLine();
}
public void testMethod(object objEvent)
{
//TODO: Add your code here
ManualResetEvent e = (ManualResetEvent)objEvent;
e.Set();
}为什么在Ubuntu上用c开线程后不执行回调函数
可以通过线程池ThreadPool来解决,使用ThreadPool.QueueUserWorkItem(回调函数,object),将参数封装在一个类的对象中,传给回调函数去执行。
TheadPool的用法:
1、创建一个ManualResetEvent的对象,就像一个信号灯,指示线程的挂起和执行;
2、ManualResetEvent对象创建时,可以指定默认状态:true为有信号,false为无信号;
3、调用Reset()方法重置状态;
4、调用WaitOne()方法,使线程处于等待状态;
5、调用Set()方法设置状态。
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections;
namespace Demo
{
public class ParamObject
{
public int number;
public ParamObject (int number)
{
this.number = number;
}
}
public class ThreadClass
{
public Hashtable aHashTable;
public ManualResetEvent aManualResetEvent;
public static int iCount = 0;
public static int iMaxCount = 0;
public ThreadClass(int maxCount)
{
aHashTable = new Hashtable(maxCount);
iMaxCount = maxCount;
}
public void ThreadRun(object aParamObject)
{
Console.WriteLine("HashCode: {0}, Number in Object: {1}", Thread.CurrentThread.GetHashCode(), ((ParamObject)aParamObject).number);
lock (aHashTable)
{
if (!aHashTable.ContainsKey(Thread.CurrentThread.GetHashCode()))
{
aHashTable.Add(Thread.CurrentThread.GetHashCode(), 0);
}
aHashTable[Thread.CurrentThread.GetHashCode()] = (int)aHashTable[Thread.CurrentThread.GetHashCode()] + 1;
}
Thread.Sleep(3000);
Interlocked.Increment(ref iCount);
if (iCount == iMaxCount)
{
Console.WriteLine("Setting aManualResetEvent...");
aManualResetEvent.Set();
}
}
}
class Program
{
public static void Main(string[] args)
{
bool enableThreadPool = false;
int iMaxCount = 20;
ManualResetEvent aManualResetEvent = new ManualResetEvent(false);
Console.WriteLine("Insert {0} items to Thread Pool.", iMaxCount);
ThreadClass aThreadClass = new ThreadClass(iMaxCount);
aThreadClass.aManualResetEvent = aManualResetEvent;
// First, add an item to check if your system supports ThreadPool API function or not.
try
{
ThreadPool.QueueUserWorkItem(new WaitCallback(aThreadClass.ThreadRun), new ParamObject(0));
enableThreadPool = true;
}
catch (NotSupportedException ex)
{
Console.WriteLine("Thread Pool API is not supported in this system.");
enableThreadPool = false;
}
if (enableThreadPool)
{
for (int i = 1; i < iMaxCount; i++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(aThreadClass.ThreadRun), new ParamObject(i));
}
Console.WriteLine("Waiting for thread pool to drain");
aManualResetEvent.WaitOne(Timeout.Infinite, true);
Console.WriteLine("Thread Pool has been drained.");
Console.WriteLine("Load threads info:");
foreach (object key in aThreadClass.aHashTable.Keys)
{
Console.WriteLine("Key: {0}, Value: {1}", key, aThreadClass.aHashTable[key]);
}
}
Console.ReadLine();
}
}
}
legionbox怎么样?legionbox是一家来自于澳大利亚的主机销售商,成立时间在2014年,属于比较老牌商家。主要提供VPS和独立服务器产品,数据中心包括美国洛杉矶、瑞士、德国和俄罗斯。其中VPS采用KVM和Xen架构虚拟技术,硬盘分机械硬盘和固态硬盘,系统支持Windows。当前商家有几款大硬盘的独立服务器,可选美国、德国和瑞士机房,有兴趣的可以看一下,付款方式有PAYPAL、BTC等。...
ftlcloud怎么样?ftlcloud(超云)目前正在搞暑假促销,美国圣何塞数据中心的云服务器低至9元/月,系统盘与数据盘分离,支持Windows和Linux,免费防御CC攻击,自带10Gbps的DDoS防御。FTL-超云服务器的主要特色:稳定、安全、弹性、高性能的云端计算服务,快速部署,并且可根据业务需要扩展计算能力,按需付费,节约成本,提高资源的有效利用率。点击进入:ftlcloud官方网站...
licloud怎么样?licloud目前提供香港cmi服务器及香港CN2+BGP服务器/E3-1230v2/16GB内存/240GB SSD硬盘/不限流量/30Mbps带宽,$39.99/月。licloud 成立於2021年,是香港LiCloud Limited(CR No.3013909)旗下的品牌,主要提供香港kvm vps,分为精简网络和高级网络A、高级网络B,现在精简网络和高级网络A。现在...
queueuserworkitem为你推荐
赛我网赛我网的号自己能封吗?怎么样免费装扮qq空间要怎么免费装扮QQ空间!二叉树遍历写出二叉树的先序遍历、中序遍历、后序遍历。淘宝店推广如何推广淘宝店云挂机有免费的云挂机软件吗?什么是云平台什么是云系统?网管工具网吧工具有什么?聚美优品红包在支付宝钱包里的聚美优品红包要怎么使用263企业邮箱设置263企业邮箱如何添加新的信箱?263企业邮箱设置263企业邮箱如何修改密码
长春域名注册 网站域名备案查询 河南vps 韩国加速器 表单样式 宁波服务器 hinet 泉州移动 香港亚马逊 cloudlink 创建邮箱 海外空间 wordpress中文主题 美国迈阿密 网站加速 乐视会员免费领取 cdn加速 隐士ddos 电脑主机 远程主机强迫关闭了一个现有的连接 更多