why happen "WaitHandles must be less than or equal to 64"
一、背景:
在一个项目中碰到大数据插入的问题,一次性插入20万条数据(SQL Server),并用200个线程去执行,计算需要花费多少时间,因此需要等200个线程处理完成后,记录花费的时间,需要考虑的一个问题是:如何判断判断多个线程是否全部执行完成。在执行数据库的插入过程中,当每个线程需要处理的数据量大时,是个耗时的过程,故对通过配置开启多个线程。
二、问题:
问题出来了,那么如何知道所有的线程操作都全部完成了,答案是利用C#中的的ManualResetEvent来处理;于是有下面的写法。
//针对每个线程 绑定初始化一个ManualResetEvent实例
ManualResetEvent doneEvent = new ManualResetEvent(false);
//通过ThreadPool.QueueUserWorkItem(网络请求方法HttpRequest,doneEvent ) 来开启多线程 //将等待事件一一加入事件列表 List<ManualResetEvent> listEvent = new List<ManualResetEvent>();
for(int i=0;i<线程数;i++){
listEvent.Add(doneEvent);
} //主线程等待每个线程全部完成
WaitHandle.WaitAll(listEvent.ToArray());
//....接下去的时间计算 //在保存数据的的每个线程中调用
doneEvent.Set();//通知主线程 本线程保存数据方法已经调用完成
运行好像没有问题,但是当线程数大于64个之后抛出异常 WaitHandles must be less than or equal to 64
通过网上查询,得知原来WaitHandle.WaitAll(listEvent.ToArray()); 这里listEvent线程数不能超过64个
三、解决方案:
原理:封装一个ManualResetEvent对象,一个计数器current,提供SetOne和WaitAll方法;
主线程调用WaitAll方法使ManualResetEvent对象等待唤醒信号;
各个子线程调用setOne方法 ,setOne每执行一次current减1,直到current等于0时表示所有子线程执行完毕 ,调用ManualResetEvent的set方法,这时主线程可以执行WaitAll之后的步骤。
目标:减少ManualResetEvent对象的大量产生和使用的简单性。
四、例子:
public class MutipleThreadResetEvent : IDisposable
{
private readonly ManualResetEvent done;
private readonly int total;
private long current;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="total">需要等待执行的线程总数</param>
public MutipleThreadResetEvent(int total)
{
this.total = total;
current = total;
done = new ManualResetEvent(false);
} /// <summary>
/// 唤醒一个等待的线程
/// </summary>
public void SetOne()
{
// Interlocked 原子操作类 ,此处将计数器减1
if (Interlocked.Decrement(ref current) == )
{
//当所以等待线程执行完毕时,唤醒等待的线程
done.Set();
}
} /// <summary>
/// 等待所以线程执行完毕
/// </summary>
public void WaitAll()
{
done.WaitOne();
} /// <summary>
/// 释放对象占用的空间
/// </summary>
public void Dispose()
{
((IDisposable)done).Dispose();
}
}
本质就是只通过1个ManualResetEvent 对象就可以实现同步N(N可以大于64)个线程
public class Process
{
public static int workItemCount = ;
Process() { }
public static void ProcessDataThread(object state)
{
State stateinfo = state as State; int count = Int32.Parse(stateinfo.perthreadtotal.ToString());
if (count == )
{
ProcessManage.InputLog("输入的数据不正确,请重新输入");
return;
}
int workItemNumber = workItemCount;
Interlocked.Increment(ref workItemCount);
ProcessManage.InputLog(string.Format("线程{0}开始工作", workItemNumber.ToString()));
string sql = @"insert into gpsposition (PlateType,CarNo,Latitude,Longitude,Altitude,Heading,Speed,Timestamp)
values";
string insertvalue = string.Empty;
string va = @"('02','渝B12345',10,10,10,20,10,'2016/11/1 12:00'),";
for (int i = ; i < count; i++)
{
insertvalue = (insertvalue + va);
}
insertvalue = insertvalue.TrimEnd(',');
//执行sql语句
try
{
ProcessManage.ProcessData(string.Concat(sql, insertvalue));
}
catch (Exception ex)
{
ProcessManage.InputLog(string.Format("线程{0},SQL执行错误:{1}", workItemNumber.ToString(), ex.Message));
}
finally
{
ProcessManage.InputLog(string.Format("线程{0}执行完成", workItemNumber.ToString()));
stateinfo.manualEvent.SetOne();
}
}
}
页面调用代码如下:
private void button1_Click(object sender, EventArgs e)
{
Process.workItemCount = ;
int threadcount = ;
int totalcount = ;
Int32.TryParse(this.txtThreadCount.Text, out threadcount);
Int32.TryParse(this.txtTotalCount.Text, out totalcount);
if (threadcount == || totalcount == )
{
MessageBox.Show("文本中输入的数字不正确,请输入大于0的整数");
return;
}
int perthreadtotal = totalcount / threadcount;
ProcessManage.InputLog(string.Format("总记录数-{0}条", totalcount));
ProcessManage.InputLog(string.Format("线程执行数量-{0}个", threadcount));
ProcessManage.InputLog(string.Format("每个线程执行记录数-{0}条", perthreadtotal));
ProcessManage.InputLog("=================================================");
ProcessManage.InputLog("开始启动线程执行"); State stateInfo;
Stopwatch watch = new Stopwatch();
watch.Start();
using (var manualEvents = new MutipleThreadResetEvent(threadcount))
{
for (int i = ; i < threadcount; i++)
{
stateInfo = new State(manualEvents, perthreadtotal);
ThreadPool.QueueUserWorkItem(new WaitCallback(Process.ProcessDataThread), stateInfo);
}
manualEvents.WaitAll();
}
watch.Stop();
ProcessManage.InputLog(string.Format("全部线程执行完成,耗时{0}秒",watch.Elapsed));
}
五、UI效果:
总结:20万数据一次性用200个线程执行,只花费了5秒多的时间即完成。
why happen "WaitHandles must be less than or equal to 64"的更多相关文章
- Reloading Java Classes 201: How do ClassLoader leaks happen? Translation
The original link : http://zeroturnaround.com/rebellabs/rjc201/ From ClassLoaders to Classes 从ClassL ...
- English trip V1 - 6.Accidents Happen! 发生意外! Teacher:Corrine Key: 过去进行时 was or were + Ving
In this lesson you will learn to talk about past occurences. 过去进行时 课上内容(Lesson) C: Hi, Loki! L: Hi, ...
- 【MyEcplise】导入项目后,会定时弹出一下错误MyEcplise tern was unable to complete your request in time.This couble happen if your project contains several large javaScript libraies.
Myecplise弹出错误如下: 错误代码: MyEcplise tern was unable to complete your request in time.This couble happen ...
- happen before 原则
并发一直都是程序开发者绕不开的难题,在上一篇文章中我们知道了导致并发问题的源头是 : 多核 CPU 缓存导致程序的可见性问题.多线程间切换带来的原子性问题以及编译优化带来的顺序性问题. 原子性问题我们 ...
- jvm(三)指令重排 & 内存屏障 & 可见性 & volatile & happen before
参考文档: https://tech.meituan.com/java-memory-reordering.html http://0xffffff.org/2017/02/21/40-atomic- ...
- 【JS】Beginner1:Making Stuff Happen
1.JS(JavaScript) is for interactivity 2.How does JS relate to HTML&CSS? script tag script elemen ...
- In ZeroDB, the client is responsible for the database logic. Data encryption, decryption, and compression also happen client side. Therefore, the server never has any knowledge about the data, its str
zerodb/index.rst at master · zerodb/zerodb https://github.com/zerodb/zerodb/blob/master/docs/source/ ...
- 进程间IPC通信-stop waiting for thing to happen,go out and make them happen!!!
进程间通信: System V IPC对象: ipcs -q:查看消息队列 ipcs -m:查看共享内存 ipcs -s:查看信号灯集 ipcrm -q:删除消息队列 ipcrm -m:删除共 ...
- 从netty-example分析Netty组件续
上文我们从netty-example的Discard服务器端示例分析了netty的组件,今天我们从另一个简单的示例Echo客户端分析一下上个示例中没有出现的netty组件. 1. 服务端的连接处理,读 ...
随机推荐
- thinkPHP(待更新)
一些函数 1. set_include_path().get_include_path() .PATH_SEPARATOR 设置php加载的路径 2. register_shutdown_func ...
- Razor视图添加命名空间
在.cshtml文件添加@using MyNamespace,只是在页面添加引用这样编译不通过,还需要在view文件夹的web.config添加引用,找到<namespaces>添加< ...
- 基于vue的新组件开发
前天完成了一个新组件的开发,做的过程也是各种遇到问题,彻底弄懂了slot,巩固了一些flex布局和jquery的知识,比起自己第一次做组件开发,现在已经是能够下手做,遇到问题解决问题,还算有进步. 但 ...
- PHP中CURL方法curl_setopt()函数的一些参数
bool curl_setopt (int ch, string option, mixed value)curl_setopt()函数将为一个CURL会话设置选项.option参数是你想要的设置,v ...
- 【Mocha.js 101】同步、异步与 Promise
前情提要 在上一篇文章<[Mocha.js 101]Mocha 入门指南>中,我们提到了如何用 Mocha.js 进行前端自动化测试,并做了几个简单的例子来体验 Mocha.js 给我们带 ...
- Swift---- 可选值类型(Optionals) 、 断言(Assertion) 、 集合 、 函数
1 使用数组实现九宫格 1.1 问题 Swift提供经典的数组和字典两种集合类型来存储集合数据.本案例使用数组实现一个九宫格程序,如图-1所示: 图-1 1.2 方案 九宫格就是有一个n行n列的方格, ...
- 嵌入式linux应用程序移植方法总结
嵌入式linux应用程序移植方法总结 前段时间一直在做openCapwap的移植和调试工作,现在工作已接近尾声,编写本文档对前段工作进行一个总结,分享下openCapwap移植过程中的经验和感悟.江浩 ...
- GWAS Simulation
comvert hmp to ped1, ped2, map fileSB1.ped, SB2.ped, SB.map 1, choose 20 markers for 30 times(WD: /s ...
- android 底层入门开发(二)
LED将为我闪烁:控制发光二极管 对于大多数Linux驱动来说,需要直接与硬件交互,本章主要介绍用Linux驱动来控制二极管的明暗,即通过Linux驱动发送数据控制开发板上LED灯的开关. 第一节介绍 ...
- 为windows应用程序提供托盘图标
1>包含头文件 #include "Shellapi.h" 2>相关结构体和函数: NOTIFYICONDATA WINSHELLAPI BOOL ...