gpt4 book ai didi

c# - 线程池中的事件线程数

转载 作者:太空狗 更新时间:2023-10-29 20:01:57 26 4
gpt4 key购买 nike

当我写下面的代码时,为什么我得到可用的线程号,如 1022、1020。我必须获得 25 个线程最大值,因为我正在使用线程池。

我猜输出的线程数是系统上可用的线程数。我需要在我的线程池中获取可用的线程号,在 win 表单应用程序中。

private void Foo()
{
int intAvailableThreads, intAvailableIoAsynThreds;

// ask the number of avaialbe threads on the pool,
//we really only care about the first parameter.
ThreadPool.GetAvailableThreads(out intAvailableThreads,
out intAvailableIoAsynThreds);

// build a message to log
string strMessage =
String.Format(@"Is Thread Pool: {1},
Thread Id: {2} Free Threads {3}",
Thread.CurrentThread.IsThreadPoolThread.ToString(),
Thread.CurrentThread.GetHashCode(),
intAvailableThreads);

// check if the thread is on the thread pool.
Trace.WriteLine(strMessage);

// create a delay...
Thread.Sleep(30000);

return;
}

非常感谢..

(注意:我从 http://www.codeproject.com/KB/cs/AsyncMethodInvocation.aspx 得到代码)好文章!!

最佳答案

I have to get 25 thread max as I am using thread pool.

随着时间的推移,每个核心线程池中的最大线程数发生了很大变化。它不再是每个内核 25 个,我怀疑这是您所期望的。

例如,在我的带有 .NET 4 的四核超线程笔记本电脑上运行它,我得到最多 32767 个工作线程和 1000 个 IO 完成端口线程:

using System;
using System.Threading;

class Test
{
static void Main()
{
int worker;
int ioCompletion;
ThreadPool.GetMaxThreads(out worker, out ioCompletion);
Console.WriteLine("{0} / {1}", worker, ioCompletion);
}
}

在 .NET 3.5 下,我有 2000 个工作线程,还有 1000 个 IO 完成端口线程。

但这并不是线程池中实际的线程数 - 它是线程池允许自己随时间创建的最大线程数。

关于c# - 线程池中的事件线程数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5236493/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com