.NET 线程池中存在哪些线程?
在 .NET 中有几种执行异步线程的方法:
System.Threading.Thread.Start()
System.Delegate.BeginInvoke()
我的问题是异步线程在每个场景中位于何处?在某些情况下,我想避免使用池线程,因为我正在执行的某些进程需要很长时间并且会占用池线程。
There are a few ways to execute asynchronous threads in .NET:
System.Threading.Thread.Start()
System.Delegate.BeginInvoke()
My question is where do asynchronous threads live in each of these scenarios? I want to in some cases avoid using pooled threads because some processes I'm executing take a long time and will obligate the pooled thread.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
运行从
Delegate.BeginInvoke
调用的代码的线程与从ThreadPool
类访问的线程存在于同一池中。创建您自己的 Thread 对象的作用就是:创建一个新的(非池化)线程。下面是验证这一点的快速方法:创建一个新的 Windows 窗体应用程序并在窗体上放置两个按钮。包含类似以下代码的内容:
当您单击第一个按钮(其处理程序创建新
Thread
对象的按钮)时,您应该会看到同时弹出 8 个对话框。当您单击第二个(调用BeginInvoke
)时,您应该会看到最多弹出 4 个对话框。前四次您通过单击“确定”关闭其中一个对话框时,当关闭的对话框的线程返回到池中时,应该会弹出另一个对话框(具有相同的线程 ID)。Threads that run code called from
Delegate.BeginInvoke
exist in the same pool as those accessed from theThreadPool
class. Creating your ownThread
object does just that: creates a new (non-pooled) thread.Here's a quick way to verify this: create a new Windows Forms application and put two buttons on the form. Include something like the following code:
When you click on the first button (the one whose handler creates new
Thread
objects), you should see 8 dialogs pop up all at once. When you click on the second one (which callsBeginInvoke
), you should see up to 4 dialogs pop up. The first four times you dismiss one of these by clicking 'OK', another dialog should pop up (with the same thread id) as the closed dialog's thread is returned to the pool.当您使用 Thread.Start() 时,该线程与线程池是分开的。当您使用 BeginInvoke 时,(IIRC) 调用将交给线程池。与 QueueUserWorkItem 相同。
When you use Thread.Start(), that thread is seperate from the ThreadPool. When you use BeginInvoke, (IIRC) the call is given to the threadpool. Same with QueueUserWorkItem.
在第一种情况下,他们无处居住。您正在手动创建一个新线程。在第二个示例中,如果有可用线程,则会从线程池中抽取一个线程。您应该避免在 ASP.NET 等大量多线程应用程序中使用线程池中的线程来进行长时间运行的操作,因为您可能会危及也用于服务请求的线程。
基本经验法则是为长时间运行的操作手动创建线程,但数量很少(因为创建线程非常耗时),并使用池中的线程来执行多个短期操作。
In the first scenario they live nowhere. You are manually creating a new thread. In the second example a thread is drawn from the thread pool if there is one available. You should avoid using threads from the thread pool in heavily multi-threaded applications like ASP.NET for long running operations because you might be jeopardizing threads which are also used to service requests.
The basic rule of thumb is to manually create threads for long running operations but few of them (because creating a thread is time-consuming) and use threads from the pool for multiple short lived operations.
涉及创建不在池中的线程。
System.Delegate.BeginInvoke() 将取决于实际的实现,可以是线程池、后台或前台线程。
如果您解释一下您的情况,我们应该能够提供更多帮助。
Involves creating a thread which is not in the pool.
System.Delegate.BeginInvoke()
would depend on the actual implementation which can be thread pool, background or foreground thread.If you explain your case, we should be able to help more.