一、背景:

在一个项目中碰到大数据插入的问题,一次性插入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"的更多相关文章

  1. Reloading Java Classes 201: How do ClassLoader leaks happen? Translation

    The original link : http://zeroturnaround.com/rebellabs/rjc201/ From ClassLoaders to Classes 从ClassL ...

  2. 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, ...

  3. 【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 ...

  4. happen before 原则

    并发一直都是程序开发者绕不开的难题,在上一篇文章中我们知道了导致并发问题的源头是 : 多核 CPU 缓存导致程序的可见性问题.多线程间切换带来的原子性问题以及编译优化带来的顺序性问题. 原子性问题我们 ...

  5. jvm(三)指令重排 & 内存屏障 & 可见性 & volatile & happen before

    参考文档: https://tech.meituan.com/java-memory-reordering.html http://0xffffff.org/2017/02/21/40-atomic- ...

  6. 【JS】Beginner1:Making Stuff Happen

    1.JS(JavaScript) is for interactivity 2.How does JS relate to HTML&CSS? script tag script elemen ...

  7. 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/ ...

  8. 进程间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:删除共 ...

  9. 从netty-example分析Netty组件续

    上文我们从netty-example的Discard服务器端示例分析了netty的组件,今天我们从另一个简单的示例Echo客户端分析一下上个示例中没有出现的netty组件. 1. 服务端的连接处理,读 ...

随机推荐

  1. 搭建java web开发环境、使用eclipse编写第一个java web程序

    开发工具:eclipse-jee-juno-SR2-win32-x86_64(请自行官网下载) 使用服务器:apache-tomcat-7.0.35-windows-x64(请自行官网下载) 打开 e ...

  2. What is the DD in java web application

    http://docs.oracle.com/cd/E13222_01/wls/docs70/webapp/webappdeployment.html

  3. Linux Shell shortcut

    Ctrl+a跳到第一个字符前Ctrl+x同上但再按一次会从新回到原位置 Details see below: Linux shell shortcut

  4. MyEclipse中直接打开class文件的方法

    安装步骤: 1>下载jad.exe(这是一个class文件的反编译工具,但是是命令行运行编译,使用起来不是很方便:), 将其拷贝到%JAVA_HOME%/bin目录下(其他目录也可). 2> ...

  5. OJ-Triangle

    这是Leet Code OJ上面的一道题,关于求从上到下的最小路径. 这是原题链接:https://leetcode.com/problems/triangle/ Given a triangle, ...

  6. wordpress模板学习之导航目录

    wordpress的导航目录分为三个部分,一开启,二配置:三使用 开启在functions.php,这个注册会保存在全局变量中,接下来在菜单配置中会看到 register_nav_menu( 'pri ...

  7. mvc4 部署http错误403.14 forbidden

    1. 检查服务器上是否安装了“HTTP重定向”功能和“静态内容压缩”功能(在添加/删除程序或增加角色处安装).这是我所遇到的问题:2. 应用程序池要被配置为“集成”3. 把.net 4.0安装在iis ...

  8. Java语言的安全性的体现

    Java语言的安全性的体现 1.严格遵循面向对象的规范.这样封装了数据细节,只提供接口给用户.增加了数据级的安全性. 2.无指针运算.java中的操作,除了基本类型都是引用的操作.引用是不能进行增减运 ...

  9. 部署点评Cat监控项目(转)

    原文地址:http://www.bubuko.com/infodetail-986338.html 在项目中监控代码运行的状况,可以采用点评的Cat项目来监控整个项目,但是按照官方的文档来部署cat, ...

  10. UE4 去除不正确的水面倒影以及不完整镜头轮廓

    最近在做的项目遇到了一点点问题,出现了如下效果 视角对着湖面移动会出现一个显示不完整的轮廓(比较长的蓝色矩形),详细一点就是下图这样,以及近处物体的倒影(从光照的照射角度来看是不应该出现的) 一开始就 ...