起因:
最近在写一个导数据的程序,需要从几个老数据表中取出n多的数据,然后加以处理再添加到新数据库的对应表中。单步操作太慢了,这不正是多线程的用武之地吗?
对于每一种数据我都得写一套类似的代码,表意代码如下
2 |
DataSet dsUser = OldDbAccess.GetOldUsers(minId); |
4 |
DataTable[] sectionUsers = new DataTable[threadCn]; |
6 |
AutoResetEvent[] evts = new AutoResetEvent[threadCn]; |
由于老数据有n种,每一种的处理方法又不一样,所以我写了n个类似上面的处理步骤,这太累了吧,于是想重构,将上面的操作步骤中相同的地方提取出来。于是有了AsyncHelper静态类,这个静态类有几个公开的静态方法来做上面那些分多个线程处理大量数据的步骤中一样的过程。
第一个方法的签名应该是这样子的
4 |
/// <param name="dataCollection">多线程操作的数据集合</param> |
5 |
/// <param name="threadCn">分多少个线程来做</param> |
6 |
/// <param name="processItemMethod">处理数据集合中单个数据使用的处理方法</param> |
7 |
public static void DoAsync(IList dataCollection, int threadCn, WaitCallback processItemMethod) |
大量的数据定义一个IList类型的类作为参数传递,要分多少个线程来处理这些数据用threadCn指定,需要注意的是WaitHandle.WaitAll方法决定了threadCn必须小于64,最后一个参数是处理大量数据中的一个数据的方法。方法的签名出来了,我在上面又做了几次重复的处理,写这个方法应该不是个问题。同样的我们也是需要把IList数据分成threadCn份,然后将每一份都交给ThreadPool来处理,这很简单。
04 |
/// <param name="dataCollection">多线程操作的数据集合</param> |
05 |
/// <param name="threadCn">分多少个线程来做</param> |
06 |
/// <param name="processItemMethod">处理数据集合中单个数据使用的处理方法</param> |
07 |
public static void DoAsync(IList dataCollection, int threadCn, WaitCallback processItemMethod) |
09 |
if (dataCollection == null ) throw new ArgumentNullException( "dataCollection" ); |
11 |
if (threadCn >= 64 || threadCn < 2) |
13 |
throw new ArgumentOutOfRangeException( "threadCn" , "threadCn 参数必须在2和64之间" ); |
16 |
if (threadCn > dataCollection.Count) threadCn = dataCollection.Count; |
18 |
IList[] colls = new ArrayList[threadCn]; |
20 |
AutoResetEvent[] evts = new AutoResetEvent[threadCn]; |
22 |
for ( int i = 0; i < threadCn; i++) |
24 |
colls[i] = new ArrayList(); |
25 |
evts[i] = new AutoResetEvent( false ); |
28 |
for ( int i = 0; i < dataCollection.Count; i++) |
30 |
object obj = dataCollection[i]; |
31 |
int threadIndex = i % threadCn; |
32 |
colls[threadIndex].Add(obj); |
35 |
for ( int i = 0; i < threadCn; i++) |
37 |
ThreadPool.QueueUserWorkItem(DoPrivate, new object [] { |
38 |
colls[i],processItemMethod,evts[i] |
42 |
WaitHandle.WaitAll(evts); |
44 |
private static void DoPrivate( object data) { |
45 |
object [] datas = data as object []; |
46 |
IList dataList = datas[0] as IList; |
47 |
WaitCallback method = datas[1]; |
48 |
AutoResetEvent evt = datas[2] as AutoResetEvent; |
50 |
foreach ( object item in dataList) |
这个很容易实现,不过既然要做封装我们就不得不多考虑一些,我们的线程好比是一个一个的侦查兵,这次给他们的任务是抓一个敌人回来问问敌情,任务要求只抓一个敌人,也就是说如果某一个侦查兵抓到一个敌人之后要给其他战友发信息,告诉他们别忙了,任务已经完成了。这个该怎么办呢,办法总是要比问题多的。
WaitHandle类有WaitAny静态方法,上面侦察兵的例子不就是个WaitAny吗,主线程需要在接受到一个线程完成的信号后通知所有线程,“任务完成了,大家都回家吧”?大家如果有兴趣的话,可以给出自己的方案,我的方案明天放出来。明天一并要解决的还有取得多个执行操作的返回值问题。
我们需要解决WaitAny和取得异步执行的返回值的问题。地球人都知道Thread和ThreadPool接受的委托都是没有返回值的。要想取的返回值,我们就得自己动手了,我们需要构造一个AsyncContext类,由这个类来保存异步执行的状态以并存储返回值。
代码如下:
2 |
using System.Collections.Generic; |
4 |
using System.Collections; |
5 |
using System.Threading; |
6 |
using System.Diagnostics; |
001 |
public delegate object DoGetObjTask( object state); |
002 |
public static class AsyncHelper |
007 |
/// <param name="dataCollection">多线程操作的数据集合</param> |
008 |
/// <param name="threadCn">分多少个线程来做</param> |
009 |
/// <param name="processItemMethod">处理数据集合中单个数据使用的处理方法</param> |
010 |
public static void DoAsync(IList dataCollection, int threadCn, WaitCallback processItemMethod) |
012 |
DoAsync(dataCollection, threadCn, processItemMethod, true ); |
019 |
/// <param name="dataCollection">多线程操作的数据集合</param> |
020 |
/// <param name="threadCn">分多少个线程来做</param> |
021 |
/// <param name="processItemMethod">处理数据集合中单个数据使用的处理方法</param> |
022 |
/// <param name="needWaitAll">是否需要等待所有线程执行完毕才返回,为true时会等待所有线程执行完毕,否则则是在有一个线程执行完毕就返回</param> |
023 |
public static void DoAsync(IList dataCollection, int threadCn, DoGetObjTask processItemMethod, bool needWaitAll, out Hashtable processResult) |
025 |
DoAsyncPrivate(dataCollection, threadCn, null , processItemMethod, needWaitAll, true , out processResult); |
031 |
/// <param name="dataCollection">多线程操作的数据集合</param> |
032 |
/// <param name="threadCn">分多少个线程来做</param> |
033 |
/// <param name="processItemMethod">处理数据集合中单个数据使用的处理方法</param> |
034 |
/// <param name="needWaitAll">是否需要等待所有线程执行完毕才返回,为true时会等待所有线程执行完毕,否则则是在有一个线程执行完毕就返回</param> |
035 |
public static void DoAsync(IList dataCollection, int threadCn, DoGetObjTask processItemMethod, out Hashtable processResult) |
037 |
DoAsyncPrivate(dataCollection, threadCn, null , processItemMethod, true , true , out processResult); |
043 |
/// <param name="dataCollection">多线程操作的数据集合</param> |
044 |
/// <param name="threadCn">分多少个线程来做</param> |
045 |
/// <param name="processItemMethod">处理数据集合中单个数据使用的处理方法</param> |
046 |
/// <param name="needWaitAll">是否需要等待所有线程执行完毕才返回,为true时会等待所有线程执行完毕,否则则是在有一个线程执行完毕就返回</param> |
047 |
public static void DoAsync(IList dataCollection, int threadCn, WaitCallback processItemMethod, bool needWaitAll) |
050 |
DoAsyncPrivate(dataCollection, threadCn, processItemMethod, null , needWaitAll, false , out hash); |
053 |
private static void DoAsyncPrivate(IList dataCollection, int threadCn, WaitCallback processItemMethod, DoGetObjTask getObjMethod, bool needWaitAll, bool hasReturnValue, out Hashtable processResult) |
055 |
if (dataCollection == null ) throw new ArgumentNullException( "dataCollection" ); |
057 |
if (threadCn >= 64 || threadCn < 2) |
059 |
throw new ArgumentOutOfRangeException( "threadCn" , "threadCn 参数必须在2和64之间" ); |
062 |
if (threadCn > dataCollection.Count) threadCn = dataCollection.Count; |
064 |
IList[] colls = new ArrayList[threadCn]; |
066 |
DataWithStateList dataWithStates = new DataWithStateList(); |
067 |
AutoResetEvent[] evts = new AutoResetEvent[threadCn]; |
069 |
for ( int i = 0; i < threadCn; i++) |
071 |
colls[i] = new ArrayList(); |
072 |
evts[i] = new AutoResetEvent( false ); |
075 |
for ( int i = 0; i < dataCollection.Count; i++) |
077 |
object obj = dataCollection[i]; |
078 |
int threadIndex = i % threadCn; |
079 |
colls[threadIndex].Add(obj); |
080 |
dataWithStates.Add( new DataWithState(obj, ProcessState.WaitForProcess)); |
083 |
AsyncContext context = AsyncContext.GetContext(threadCn, dataWithStates, needWaitAll, hasReturnValue, processItemMethod, getObjMethod); |
085 |
for ( int i = 0; i < threadCn; i++) |
087 |
ThreadPool.QueueUserWorkItem(DoPrivate, new object [] { |
088 |
colls[i],context,evts[i] |