gpt4 book ai didi

c# - 需要帮助了解 .net ThreadPool

转载 作者:行者123 更新时间:2023-11-30 15:45:35 25 4
gpt4 key购买 nike

我想了解 ThreadPool 的作用,我有这个 .NET 示例:

class Program
{
static void Main()
{
int c = 2;

// Use AutoResetEvent for thread management

AutoResetEvent[] arr = new AutoResetEvent[50];

for (int i = 0; i < arr.Length; ++i)
{
arr[i] = new AutoResetEvent(false);
}

// Set the number of minimum threads
ThreadPool.SetMinThreads(c, 4);

// Enqueue 50 work items that run the code in this delegate function
for (int i = 0; i < arr.Length; i++)
{
ThreadPool.QueueUserWorkItem(delegate(object o)
{
Thread.Sleep(100);
arr[(int)o].Set(); // Signals completion

}, i);
}

// Wait for all tasks to complete
WaitHandle.WaitAll(arr);
}
}

这是否运行 50 个“任务”,以 2 个为一组 (int c) 直到它们全部完成?或者我不明白它的真正作用。

最佳答案

如果你有一点时间,我真的会推荐这本书:

http://www.albahari.com/threading/

这是一本很好的读物,它奠定了从基本线程到并行编程的基础和工作方式。我建议您在尝试修改线程池代码之前对前两章有一个基本的了解! :)

关于c# - 需要帮助了解 .net ThreadPool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5072355/

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