Thread是由线程池管理的吗?
我知道 CLR 为每个 AppDomain
ThreadPool
提供工作时间片,但我想知道是否通过像这样创建一个新线程 Thread t = new Thread( ...);
它是由 CLR 还是由 AppDomin ThreadPool
管理?
I know that the CLR gives each AppDomain
ThreadPool
time slice to work , yet i wanted to know if by creating a new thread like so Thread t = new Thread(...);
Is it managed by the CLR or by the AppDomin ThreadPool
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Thread t = new Thread();
不会由ThreadPool
管理。但它是 CLR 在操作系统线程上提供的抽象。 ThreadPool 是一个附加的抽象,有助于重用线程和共享线程资源。以下是有关 .NET 中线程的优秀资源:http://www.albahari.com/threading/
如果您使用 .NET 4.0,请考虑使用 TPL。
Thread t = new Thread();
will not be managed by theThreadPool
. But it is an abstraction provided by the CLR on the Operating System threads. ThreadPool is an addtional abstraction which facilitates reusing threads and sharing thread resources.Here is an excellent resource on threads in .NET: http://www.albahari.com/threading/
If you're using .NET 4.0 consider using TPL.
当您使用
Thread
类创建线程时,您就处于控制之中。您可以根据需要创建它们,并定义它们是否 后台或前台(保持调用进程处于活动状态),您可以设置它们的优先级
,您启动和停止它们。使用
ThreadPool
或 < a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.aspx" rel="nofollow">任务
(其中使用ThreadPool
在后台)您可以让ThreadPool
类管理线程的创建,并最大限度地提高线程的可重用性,从而节省您创建新线程所需的时间。需要注意的一件事是,与Thread
默认值不同,由ThreadPool
创建的线程不会使调用进程保持活动状态。使用
ThreadPool
的一个巨大优势是您可以使用少量线程处理大量任务。相反,考虑到池不会杀死线程(因为它是为可重用性而设计的),如果您有一堆由ThreadPool
创建的线程,但后来项目数量减少了,ThreadPool
闲置很多,浪费资源。When you create threads with the
Thread
class, you are in control. You create them as you need them, and you define whether they are background or foreground (keeps the calling process alive), you set theirPriority
, you start and stop them.With
ThreadPool
orTask
(which use theThreadPool
behind the scenes) you let theThreadPool
class manage the creation of threads, and maximizes reusability of threads, which saves you the time needed to create a new thread. One thing to notice is that unlike theThread
default, threads created by theThreadPool
don't keep the calling process alive.A huge advantage of using the
ThreadPool
is that you can have a small number of threads handle lots of tasks. Conversely, given that the pool doesn't kill threads (because it's designed for reusability), if you had a bunch of threads created by theThreadPool
, but later the number of items shrinks, theThreadPool
idles a lot, wasting resources.当您创建新线程时,它们不由线程池管理。
When you create new threads they are not managed by the thread pool.
如果您手动创建线程,则可以控制其生命周期,这与线程池无关。
If you create a thread manually then you control its life time, this is independent from the Thread pool.