10 Parallel.Invoke, MaxDegreeOfParallelism ( - ), sheduler , rougly
var actions = new [] { action, action, action, action, action, action, action, action };
var tasks = new Task[actions.Length];
for (int index = 1; index < tasks.Length; ++index)
tasks[index] = Task.Factory.StartNew(actions[index]);
tasks[0] = new Task(actions[0]);
tasks[0].RunSynchronously();
Task.WaitAll(tasks);
, Task.Factory.StartNew.
int th, io;
ThreadPool.GetMaxThreads(out th, out io);
Console.WriteLine(th);
, 32767. , , Parallel.Invoke ( ), . 8 .
, ? , - . . , Task.Factory.StartNew, , . .
, (repeat) 8 7, , 8- (16 ).
UPDATE, . . . max, . . :
int th, io;
ThreadPool.GetMinThreads(out th, out io);
(, 8). , , , - . , - ( , , , 500 ).
, , 2-3 . 0,3 . , 8 , 500 9-. ( ) 8 , , .
, :
static void Main()
{
ThreadLocal<string> ThreadName = new ThreadLocal<string>(() =>
{
return "Thread" + Thread.CurrentThread.ManagedThreadId;
});
Action action = () =>
{
bool repeat = ThreadName.IsValueCreated;
Console.WriteLine("ThreadName = {0} {1}", ThreadName.Value, repeat ? "(repeat)" : "");
Thread.Sleep(1000000);
};
int th, io;
ThreadPool.GetMinThreads(out th, out io);
Console.WriteLine("cpu:" + Environment.ProcessorCount);
Console.WriteLine(th);
Parallel.Invoke(Enumerable.Repeat(action, 100).ToArray());
ThreadName.Dispose();
Console.ReadKey();
}
, ( , ), , .
, :
int th, io;
ThreadPool.GetMinThreads(out th, out io);
ThreadPool.SetMinThreads(100, io);
( 100 ), .