この例ではParallel.For
、関数を一度に実行できるスレッドの数を10に制限したい場合、これはループの正しい使用法DoWork
ですか?10個のスレッドの1つが使用可能になるまで、他のスレッドはブロックされますか?そうでない場合でも、その関数を6000回以上実行できる、より優れたマルチスレッドソリューションは何ですか?
class Program
{
static void Main(string[] args)
{
ThreadExample ex = new ThreadExample();
}
}
public class ThreadExample
{
int limit = 6411;
public ThreadExample()
{
Console.WriteLine("Starting threads...");
int temp = 0;
Parallel.For(temp, limit, new ParallelOptions { MaxDegreeOfParallelism = 10 }, i =>
{
DoWork(temp);
temp++;
});
}
public void DoWork(int info)
{
//Thread.Sleep(50); //doing some work here.
int num = info * 5;
Console.WriteLine("Thread: {0} Result: {1}", info.ToString(), num.ToString());
}
}