开发者

Is Thread managed by the thread pool?

开发者 https://www.devze.com 2023-04-10 02:17 出处:网络
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(...);

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 T开发者_C百科hreadPool ?


Thread t = new Thread(); will not be managed by the ThreadPool. 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.


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 their Priority, you start and stop them.

With ThreadPool or Task (which use the ThreadPool behind the scenes) you let the ThreadPool 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 the Thread default, threads created by the ThreadPool 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 the ThreadPool, but later the number of items shrinks, the ThreadPool 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号