queueuserworkitem为什么在Ubuntu上用c开线程后不执行回调函数

queueuserworkitem  时间:2021-01-17  阅读:()

怎么判断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(); } } }

搬瓦工VPS:高端线路,助力企业运营,10Gbps美国 cn2 gia,1Gbps香港cn2 gia,10Gbps日本软银

搬瓦工vps(bandwagonhost)现在面向中国大陆有3条顶级线路:美国 cn2 gia,香港 cn2 gia,日本软银(softbank)。详细带宽是:美国cn2 gia、日本软银,都是2.5Gbps~10Gbps带宽,香港 cn2 gia为1Gbps带宽,搬瓦工是目前为止,全球所有提供这三种带宽的VPS(云服务器)商家里面带宽最大的,成本最高的,没有第二家了! 官方网站:https...

选择Vultr VPS主机不支持支付宝付款的解决方案

在刚才更新Vultr 新年福利文章的时候突然想到前几天有网友问到自己有在Vultr 注册账户的时候无法用支付宝付款的问题,当时有帮助他给予解决,这里正好顺带一并介绍整理出来。毕竟对于来说,虽然使用的服务器不多,但是至少是见过世面的,大大小小商家的一些特性特征还是比较清楚的。在这篇文章中,和大家分享如果我们有在Vultr新注册账户或者充值购买云服务器的时候,不支持支付宝付款的原因。毕竟我们是知道的,...

wordpress外贸企业主题 wordpress经典外贸企业建站主题

WordPress经典外贸企业建站主题,经典配色扁平化简约设计+跨屏自适应移动端设备,特色外贸企业建站功能模块+在线Inquiry询单功能,更有利于Google等英文搜索优化和站点收录。采用标准的HTML5+CSS3语言开发,兼容当下的各种主流浏览器: IE 6+(以及类似360、遨游等基于IE内核的)、Firefox、Google Chrome、Safari、Opera等;同时支持移动终端的常用...

queueuserworkitem为你推荐
易pc华硕的易PC怎么样,能流畅运行的游戏大概是什么水平的?外网和内网内网和外网有什么区别啊?绵阳电信绵阳电信宽带怎么收费的windows优化大师怎么用windows优化大师怎么用﹖1433端口怎么去看1433端口唱吧电脑版官方下载电脑上可以安装唱吧吗?腾讯文章怎么在手机腾讯网发文章怎么点亮qq空间图标怎样点亮qq空间的图标免费免费建站电脑上有真正免费的网站吗??idc前线怎么知道我电脑是3兆的宽带?
踢楼 fastdomain 美国翻墙 私服服务器 新站长网 小米数据库 电子邮件服务器 bgp双线 怎么测试下载速度 网络空间租赁 鲁诺 购买国外空间 Updog shopex主机 如何建立邮箱 可外链的相册 服务器硬件配置 卡巴斯基官网下载 hostease 乐视会员免费领取 更多