threadpool的SetMaxThreads属性好像没有影响,那为什么会在这里呢?
SetMaxThreads property of threadpool seems to has no impact, then why it is here?
这是我正在测试的简单程序threadpool.setmaxthreads属性,但似乎真的没有影响
class Program
{
static void Main(string[] args)
{
ThreadPool.SetMaxThreads(2, 0);
for (int i = 0; i < 5; i++)
{
new Thread(Go).Start();
// Thread.Sleep(20000);
}
}
static void Go()
{
Console.WriteLine("From Thread#"+ Thread.CurrentThread.ManagedThreadId );
Console.ReadLine();
}
}
}
输出
From Thread#10
From Thread#11
From Thread#12
From Thread#13
From Thread#14
您没有使用线程池,而是启动了不受线程池管理的新线程。要启动ThreadPool线程,可以使用ThreadPool.QueueUserWorkItem方法。
ThreadPool.SetMaxThreads方法的参数无效,所以方法returnsfalse,表示修改不成功
You cannot set the maximum number of worker threads or I/O completion threads to a number smaller than the number of processors on the computer. To determine how many processors are present, retrieve the value of the Environment.ProcessorCount property. In addition, you cannot set the maximum number of worker threads or I/O completion threads to a number smaller than the corresponding minimum number of worker threads or I/O completion threads. To determine the minimum thread pool size, call the GetMinThreads method.
这里是修改后的演示代码:
class Program
{
static void Main(string[] args)
{
int a; int b;
ThreadPool.GetMinThreads(out a, out b); //a=4, b=4 on my machine
bool suc = ThreadPool.SetMaxThreads(4, 4);
for (int i = 0; i < 15; i++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(
(o) =>
{
Go();
}));
}
Console.ReadLine();
}
static void Go()
{
Console.WriteLine("From Thread#" + Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(200);
Console.WriteLine("End Thread#" + Thread.CurrentThread.ManagedThreadId);
}
}
这是我正在测试的简单程序threadpool.setmaxthreads属性,但似乎真的没有影响
class Program
{
static void Main(string[] args)
{
ThreadPool.SetMaxThreads(2, 0);
for (int i = 0; i < 5; i++)
{
new Thread(Go).Start();
// Thread.Sleep(20000);
}
}
static void Go()
{
Console.WriteLine("From Thread#"+ Thread.CurrentThread.ManagedThreadId );
Console.ReadLine();
}
}
}
输出
From Thread#10
From Thread#11
From Thread#12
From Thread#13
From Thread#14
您没有使用线程池,而是启动了不受线程池管理的新线程。要启动ThreadPool线程,可以使用ThreadPool.QueueUserWorkItem方法。
ThreadPool.SetMaxThreads方法的参数无效,所以方法returnsfalse,表示修改不成功
You cannot set the maximum number of worker threads or I/O completion threads to a number smaller than the number of processors on the computer. To determine how many processors are present, retrieve the value of the Environment.ProcessorCount property. In addition, you cannot set the maximum number of worker threads or I/O completion threads to a number smaller than the corresponding minimum number of worker threads or I/O completion threads. To determine the minimum thread pool size, call the GetMinThreads method.
这里是修改后的演示代码:
class Program
{
static void Main(string[] args)
{
int a; int b;
ThreadPool.GetMinThreads(out a, out b); //a=4, b=4 on my machine
bool suc = ThreadPool.SetMaxThreads(4, 4);
for (int i = 0; i < 15; i++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(
(o) =>
{
Go();
}));
}
Console.ReadLine();
}
static void Go()
{
Console.WriteLine("From Thread#" + Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(200);
Console.WriteLine("End Thread#" + Thread.CurrentThread.ManagedThreadId);
}
}