110,538
社区成员
发帖
与我相关
我的任务
分享
class ThreadPoolTest2
{
public void Test()
{
int sum = 0;
Action<object> action = obj =>
{
Parm p = (Parm)obj;
sum += p.Num * p.Num;
p.Are.Set();
//Console.WriteLine(p.Num + ":" + p.Num * p.Num);
};
int[] array = new int[] { 1, 2, 3, 4, 5,6,7,8,9,10 };
AutoResetEvent[] autos = new AutoResetEvent[array.Length];
for (int i = 0; i < array.Length; i++)
{
autos[i] = new AutoResetEvent(false);
ThreadPool.QueueUserWorkItem(a=>action(a), new Parm() { Are = autos[i], Num = array[i] });
}
Console.WriteLine(sum);
//WaitHandle.WaitAll(autos);
autos.ToList().ForEach(s=>s.WaitOne());
Console.WriteLine(sum);
}
class Parm
{
public int Num { set; get; }
public AutoResetEvent Are { set; get; }
}
}
ThreadPoolTest2 threadPoolTest2 = new ThreadPoolTest2();
threadPoolTest2.Test();
Console.ReadKey();
autos[i] = new AutoResetEvent(false);
ThreadPool.QueueUserWorkItem(a=>action(a), new Parm() { Are = autos[i], Num = array[i] });
这两行中间要加个等待时间
(new int[] { 1, 2, 3, 4, 5,6,7,8,9,10 }).Select(s=>s*s).Sum();
是这个意思吗?