使用线程池和普通线程有什么区别? [英] What's the difference between using the thread pool and a normal thread?

查看:644
本文介绍了使用线程池和普通线程有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关SO的随机问题和答案,并遇到了这个问题:

I Was reading random questions and answers here on SO and came across this question:

C#,IAsyncResult和线程池

询问的问题是使用线程池还是使用普通线程的X方法.

The question is asked is X method using the thread pool or using normal threads.

使用线程池和普通线程有什么区别?

What's the difference between using the thread pool and a normal thread?

推荐答案

线程池通常适合于短期运行的任务.它有一个局限性,即它是应用程序范围内有限的资源(每个CPU 25个资源),并且有许多内部类使用线程池,因此,如果您执行许多长时间运行的任务,则会用尽所有线程.

The threadpool is typically suitable for short running tasks. It has the limitation that it is a application-wide limited resource (25 per CPU), and there are a lot of internal classes that use the threadpoool, so if you perform a lot of long running tasks you will use up all the threads.

对于长时间运行的任务,最好使用手动创建的线程,其后台属性设置为true.

For long running tasks, it's better to use a manually created thread, with its background property set to true.

注意:在.NET Framework 2.0版中,Thread.CurrentPrincipal属性值传播到使用QueueUserWorkItem方法排队的工作线程.在早期版本中,主体信息不会传播.

Note: In the .NET Framework version 2.0, the Thread.CurrentPrincipal property value is propagated to worker threads queued using the QueueUserWorkItem method. In earlier versions, the principal information is not propagated.

何时不使用线程池线程

在几种情况下,创建和管理自己的线程而不是使用线程池线程是合适的:

There are several scenarios in which it is appropriate to create and manage your own threads instead of using thread pool threads:

  • 您需要一个前台线程(!).

  • You require a foreground thread(!).

您需要一个线程具有特定的优先级.

You require a thread to have a particular priority.

您有一些任务导致线程长时间阻塞.线程池具有最大数量的线程,因此大量阻塞的线程池线程可能会阻止任务启动.

You have tasks that cause the thread to block for long periods of time. The thread pool has a maximum number of threads, so a large number of blocked thread pool threads might prevent tasks from starting.

您需要将线程放入单线程单元中.所有ThreadPool线程都在多线程单元中.

You need to place threads into a single-threaded apartment. All ThreadPool threads are in the multithreaded apartment.

您需要具有与线程关联的稳定身份,或将线程专用于任务.

You need to have a stable identity associated with the thread, or to dedicate a thread to a task.

一个很大的区别是线程池线程上的未处理异常会终止该过程;除了以下三个例外:

One big difference is that Unhandled exceptions on thread pool threads terminate the process; with these three exceptions:

  • 由于调用了Abort,因此在线程池线程中引发了ThreadAbortException.

  • A ThreadAbortException is thrown in a thread pool thread, because Abort was called.

由于正在卸载应用程序域,因此在线程池线程中引发了AppDomainUnloadedException.

An AppDomainUnloadedException is thrown in a thread pool thread, because the application domain is being unloaded.

公共语言运行时或宿主进程终止线程.

The common language runtime or a host process terminates the thread.

一些好的参考文献是:

托管线程池

C#中的线程

在.NET Framework中编程线程池

更新:回应评论.您可以使用GetAvailableThreads方法在任何给定时间确定线程池中的实际线程数. ThreadPool.GetMaxThreads是不同的数量.它是请求排队之前在池中允许的最大值,而不是当前在池中的实际线程数.

Update: in response to comments. You can use the GetAvailableThreads method to determine the actual number of threads in the thread pool at any given time. ThreadPool.GetMaxThreads is a different quantity. Its the max allowed in the pool before requests are queued, not the actual number of threads currently in the pool.

这篇关于使用线程池和普通线程有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆