.NET 2.0 で Parallel.For
.NET 4.0 に Parallel.For が入るのはいいけど、普及するのにどんだけかかるんだよと思ったら手が動いていた(ぉ)
using System; using System.Threading; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Parallel.For(0, 10, x => { Console.Write("{0} ", x); Thread.Sleep(1000); }); } } } namespace System.Threading { class Parallel { public static void For(int fromInclusive, int toExclusive, Action<int> body) { var threadCount = Environment.ProcessorCount; Func<int, int[]> Range = i => { Func<int, int> DividingPoint = j => { return fromInclusive + (toExclusive - fromInclusive) * j / threadCount; }; return new int[] { DividingPoint(i), DividingPoint(i + 1) }; }; var threads = new Thread[threadCount]; for (var i = 0; i < threadCount; i++) { threads[i] = new Thread(new ParameterizedThreadStart(obj => { for (int j = ((int[])obj)[0]; j < ((int[])obj)[1]; j++) { body(j); } })); } for (var i = 0; i < threadCount; i++) threads[i].Start(Range(i)); for (var i = 0; i < threadCount; i++) threads[i].Join(); } } } // For .NET Framework 2.0 Only namespace System { public delegate TResult Func<T, TResult>(T arg); }