shyaway

C# > ThreadPool Basic 본문

.NET

C# > ThreadPool Basic

shyaway 2018. 8. 20. 09:37

ThreadPool


It's in System.Threading namespace. It provides a pool of threads. Pool means some contents in some space that is big enough to see with one slight look ( typically rectangular shape ). If the content is water, then the pool becomes swimming pool, and if the inside stuff is a series of billiard balls, then it becomes a pool table. That's the general picture of pool.


Thus ThreadPool means the space where threads live inside. You can allocate some asynchronous I/O tasks or some specific jobs through ThreadPool and offload the work items to one of the threads in the thread pool.




The pros of ThreadPool

    • You don't have to create your own Thread which is computationally very expensive because there're a few threads that ThreadPool had created.
    • You can use ThreadPool API to allocate a delegate for ThreadPool to execute it later automatically. 
    • Not only ThreadPool offloads the delegate to a thread, it also manages the life cycle of threads.
    • Creating and deleting threads are on demand. It automatically creates one or deletes one when it thinks it's necessary.


The cons of ThreadPool

    • It may not be suitable for heavy weight, slow jobs. You may need a dedicated, your-own created thread for that.
    • If a task takes longer than a second, it's recommended to create your own thread.
    • You can't wait a task or cancel it unlike TPL ( Task Parallel Library )
    • You just can't reorder the jobs in the queue because it's FIFO.


ThreadPool example

Let's see the simple example of using ThreadPool.


Via ThreadPool.QueueUserWorkItem(LightweightThread), static void function has been passed into the queue as a delegate. Now the ThreadPool allocates the delegate to a thread and gets the thread started and then when the job is done, ThreadPool gets the thread ready again for accepting new, another requests. It's like renting a thread registered in ThreadPool when dequeueing an item from the queue in ThreadPool. 

Quiz

what if you delete the Thread.Sleep(1000) there?

=> The main thread of Main() will terminate before ThreadPool executes the static void method. Since ThreadPool uses a background thread, after the foreground thread has exited, the background thread is no longer able to survive.




What uses ThreadPool

Other than explicitly using the ThreadPool API, there're more things using ThreadPool. Typically, below.

    • When you create Task or Task<Result>Task for asynchronous jobs, basically the Task is scheduled to run a thread in ThreadPool.
    • Asynchronous timer also uses ThreadPool. ThreadPool executes a callback from System.Threading.Timer class and so does from System.Timers.Timer class.


Something you should know

When ThreadPool reuses a thread, it doesn't clear away the data in the thread local storage or ThreadStaticAttribute attribute. Thus if your method looks at the attribute or the storage, there might be some values left over that the previous job in the thread used. And the ThreadPool exists per a process, from .NET 4.0 the default size of ThreadPool can be vary from environment to environment. ( Some set 4 for minimum threads, others 8 for them )
































Comments