精易论坛

标题: C#线程池求简单例子 [打印本页]

作者: 陽陽陽    时间: 2023-7-12 11:31
标题: C#线程池求简单例子
本帖最后由 陽陽陽 于 2023-7-13 09:21 编辑

问题:代码不按设想的工作:

易语言代码:
  
窗口程序集名保 留  保 留备 注
窗口程序集_启动窗口   
变量名类 型数组备 注
鱼刺类_线程池EX鱼刺类_线程池Ex  

子程序名返回值类型公开备 注
__启动窗口_创建完毕  
变量名类 型静态数组备 注
n整数型 
鱼刺类_线程池EX.创建 (1, 4, , , )
计次循环首 (10, n)
如果真 (取反 (鱼刺类_线程池EX.投递任务 (&test, n)))
调试输出 (“failed”)

计次循环尾 ()
子程序名返回值类型公开备 注
test  
参数名类 型参考可空数组备 注
n整数型
调试输出 (“entering”, n)
延时 (2000)
调试输出 (“exit”, n)


i支持库列表   支持库注释   
spec特殊功能支持库



但是到了C#,New Bing的代码有有问题了:
[C#] 纯文本查看 复制代码
using System;
using System.Threading;

namespace ThreadPoolExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int maxWorkerThreads, maxCompletionPortThreads;
            ThreadPool.GetMaxThreads(out maxWorkerThreads, out maxCompletionPortThreads);
            ThreadPool.SetMaxThreads(5, maxCompletionPortThreads);

            for (int i = 0; i < 20; i++)
            {
                ThreadPool.QueueUserWorkItem(new WaitCallback(Task), i);
            }

            Console.ReadLine();
        }

        static void Task(object state)
        {
            Console.WriteLine("Task {0} is running on a thread from the thread pool.", state);
            Thread.Sleep(1000);            Console.WriteLine("Task {0} exited.", state);
        }
    }
}



会输出:
Task 18 is running on a thread from the thread pool.
Task 4 is running on a thread from the thread pool.
Task 13 is running on a thread from the thread pool.
Task 19 is running on a thread from the thread pool.
Task 16 is running on a thread from the thread pool.
Task 8 is running on a thread from the thread pool.
Task 1 is running on a thread from the thread pool.
Task 3 is running on a thread from the thread pool.
Task 2 is running on a thread from the thread pool.
Task 9 is running on a thread from the thread pool.
Task 14 is running on a thread from the thread pool.
Task 17 is running on a thread from the thread pool.
Task 0 is running on a thread from the thread pool.
Task 5 is running on a thread from the thread pool.
Task 10 is running on a thread from the thread pool.
Task 11 is running on a thread from the thread pool.
Task 12 is running on a thread from the thread pool.
Task 7 is running on a thread from the thread pool.
Task 6 is running on a thread from the thread pool.
Task 15 is running on a thread from the thread pool.
Task 8 exited.
Task 3 exited.
Task 2 exited.
Task 4 exited.
Task 18 exited.
Task 0 exited.
Task 1 exited.
Task 5 exited.
Task 13 exited.
Task 19 exited.
Task 16 exited.
Task 9 exited.
Task 14 exited.
Task 10 exited.
Task 11 exited.
Task 12 exited.
Task 7 exited.
Task 6 exited.
Task 15 exited.
Task 17 exited.

明明已经达到了最大上限,为什么它还会在那进入线程?

感谢各位大佬的答疑,非常谢谢。

ps:我发帖肯定就是不需要New Bing, ChatGPT那种晦涩难懂,各种专有名词,带有错误的答案(而且我都问过了),我希望我得到的答案是简单易懂的,没有错误的,感谢感谢感谢。




作者: 江畔孤舟    时间: 2023-7-12 11:31
c#线程池好像已经过时了. 使用task和taskFactory代替了,官方文档说是更安全,更好管理https://learn.microsoft.com/zh-cn/dotnet/api/system.threading.tasks.taskfactory?view=netframework-4.8
作者: 陽陽陽    时间: 2023-7-13 01:55
本帖最后由 陽陽陽 于 2023-7-13 09:20 编辑

真的没人吗
作者: 梦寻简单    时间: 2023-7-15 18:39
这三行代码应该能解决你的疑惑:
int minWorkerThreads, minCompletionPortThreads;
ThreadPool.GetMinThreads(out minWorkerThreads, out minCompletionPortThreads);
Console.WriteLine("minWorkerThreads:{0}, minCompletionPortThreads:{1}", minWorkerThreads, minCompletionPortThreads);
作者: 陽陽陽    时间: 2023-8-8 23:45
问题解决:线程池线程数不能超过CPU的下限




欢迎光临 精易论坛 (https://125.confly.eu.org/) Powered by Discuz! X3.4