日期:2014-05-17 浏览次数:20440 次
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] Args)
{
int[] data = Enumerable.Range(1, 1000000).ToArray();
double[] result = new double[10];
ManualResetEvent[] manualEvents = result.Select(x => new ManualResetEvent(false)).ToArray();
for (int i = 0; i < 10; i++)
{
var ts = new ThreadStart(() =>
{
result[i] = data.Skip(i * 100000).Take(100000).Select(x => (double)x).Sum();
manualEvents[i].Set();
});
new Thread(ts).Start();
}
WaitHandle.WaitAll(manualEvents.ToArray());
Console.WriteLine(result.Sum());
}
}
}