控制 dotnet core 的 ThreadPool 中的线程数
Control number of threads in dotnet core's ThreadPool
我 运行 我的 Mac 上的 dotnet 核心版本 2.1.3OS 我试过使用默认线程池但没有成功。我尝试通过多种方式修改线程数(其中大部分我都用谷歌搜索过):
Program.cs
public static void Main(string[] args)
{
ThreadPool.SetMinThreads(1, 1);
ThreadPool.SetMaxThreads(1, 1);
DoSomethingAsync();
}
.csproj
<PropertyGroup>
<ServerGarbageCollection>true</ServerGarbageCollection>
<ConcurrentGarbageCollection>true</ConcurrentGarbageCollection>
<RetainVMGarbageCollection>true</RetainVMGarbageCollection>
<ThreadPoolMinThreads>1</ThreadPoolMinThreads>
<ThreadPoolMaxThreads>1</ThreadPoolMaxThreads>
</PropertyGroup>
环境变量
$ ComPlus_ThreadPool_ForceMinWorkerThreads=1 ComPlus_ThreadPool_ForceMaxWorkerThreads=1 dotnet run
None 这些似乎有任何效果:
$ ps -M 97046
USER PID TT %CPU STAT PRI STIME UTIME COMMAND
XXX 97046 s002 0.0 S 31T 0:00.02 0:00.07 dotnet exec /XXX
97046 0.0 S 31T 0:00.00 0:00.00
97046 0.0 S 31T 0:00.00 0:00.00
97046 0.0 S 31T 0:00.00 0:00.00
97046 0.0 S 31T 0:00.00 0:00.00
97046 0.0 S 31T 0:00.00 0:00.00
97046 0.0 S 31T 0:00.00 0:00.00
97046 0.0 S 31T 0:00.00 0:00.00
我在这里错过了什么? OS具体吗?
The docs 状态:
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.
鉴于您有 8 个处理器,因此不能将其设置为只有一个。
此外,调用:
ThreadPool.SetMaxThreads(1, 1);
不会将您的 进程 限制为单个线程(即使它允许您将其设置为 1,但它不会,因为 1 小于 8)。它只是限制 线程池 的大小。该程序可以(并且将)仍然使用多个线程。
例如,GC 可能会 运行 在单独的线程上。或者您可以创建自己的独立线程(不使用线程池)。限制线程池的最大大小不会影响它。
我 运行 我的 Mac 上的 dotnet 核心版本 2.1.3OS 我试过使用默认线程池但没有成功。我尝试通过多种方式修改线程数(其中大部分我都用谷歌搜索过):
Program.cs
public static void Main(string[] args)
{
ThreadPool.SetMinThreads(1, 1);
ThreadPool.SetMaxThreads(1, 1);
DoSomethingAsync();
}
.csproj
<PropertyGroup>
<ServerGarbageCollection>true</ServerGarbageCollection>
<ConcurrentGarbageCollection>true</ConcurrentGarbageCollection>
<RetainVMGarbageCollection>true</RetainVMGarbageCollection>
<ThreadPoolMinThreads>1</ThreadPoolMinThreads>
<ThreadPoolMaxThreads>1</ThreadPoolMaxThreads>
</PropertyGroup>
环境变量
$ ComPlus_ThreadPool_ForceMinWorkerThreads=1 ComPlus_ThreadPool_ForceMaxWorkerThreads=1 dotnet run
None 这些似乎有任何效果:
$ ps -M 97046
USER PID TT %CPU STAT PRI STIME UTIME COMMAND
XXX 97046 s002 0.0 S 31T 0:00.02 0:00.07 dotnet exec /XXX
97046 0.0 S 31T 0:00.00 0:00.00
97046 0.0 S 31T 0:00.00 0:00.00
97046 0.0 S 31T 0:00.00 0:00.00
97046 0.0 S 31T 0:00.00 0:00.00
97046 0.0 S 31T 0:00.00 0:00.00
97046 0.0 S 31T 0:00.00 0:00.00
97046 0.0 S 31T 0:00.00 0:00.00
我在这里错过了什么? OS具体吗?
The docs 状态:
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.
鉴于您有 8 个处理器,因此不能将其设置为只有一个。
此外,调用:
ThreadPool.SetMaxThreads(1, 1);
不会将您的 进程 限制为单个线程(即使它允许您将其设置为 1,但它不会,因为 1 小于 8)。它只是限制 线程池 的大小。该程序可以(并且将)仍然使用多个线程。
例如,GC 可能会 运行 在单独的线程上。或者您可以创建自己的独立线程(不使用线程池)。限制线程池的最大大小不会影响它。