在此示例中,Parallel.For
如果我想将一次可以执行该功能的线程数限制DoWork
为 10 个,那么循环的正确使用是否正确?是否会阻塞其他线程,直到十个线程之一可用?如果不是,有什么更好的多线程解决方案仍然可以让我执行该功能 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());
}
}