强制ThreadPool更快启动线程 [英] Force ThreadPool to start the thread sooner
问题描述
我使用ThreadPool.QueueUserWorkItem在Windows CE上创建线程(我使用.NET Framework 3.5).有时,线程等待某些东西,并且开始得太晚了.在QueueUserWorkItem文档中,它表示将在线程池线程可用时"执行委托.
I use ThreadPool.QueueUserWorkItem for creating a thread on Windows CE (I use .NET Framework 3.5). Sometimes the thread waits for something and starts too late. In the QueueUserWorkItem documentation it says that the delegate will be executed "when a thread pool thread becomes available".
是否有一种方法可以强制ThreadPool立即执行我的委托? Thread.Start()是否可以解决这个问题?
Is there a way to force the ThreadPool to execute my delegate immediately? Would Thread.Start() be a solution for this?
谢谢!
推荐答案
首先,QueueUserWorkItem
不会创建线程,它只是在ThreadPool的队列中放置了一个任务",供工人拾取并执行.在饱和的情况下(任务多于可用线程),无法保证何时可以使用工作程序来执行任务.如果要立即执行,请使用Thread
的实例.使用ThreadPool改善赔率的唯一方法是增加工人人数.
First off, QueueUserWorkItem
doesn't create a thread, it merely places a "task" in the ThreadPool's queue for the workers to pick up and execute. In case of saturation (more tasks than available threads), there is no guarantee of when a worker will become available to execute the task. If you want immediate execution use an instance of Thread
instead. The only way to improve your odds with the ThreadPool is to increase the number of workers.
编辑:需要明确的是,如果线程池线程确实是空闲的,则它们将比开始新线程更快地接管并执行工作.
Edit: Just to be clear, if thread pool threads are indeed free, they will pick up work and execute it usually faster than starting a fresh thread.
这篇关于强制ThreadPool更快启动线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!