Module 5: Threadpools & Asynchronous Programming Flashcards
Why can singular threads create an issue?
- Threads consume resources during creation/initialization/context switching
- Number of threads increases = each thread does less actual work = decreased efficiency
- Overhead of threads surpasses system’s capacity = system crash
How do thread pools work?
- Thread pool manages a pool of worker threads & assigns them to execute the work items/tasks.
What are asynchronous calls?
- Programming mechanism that allows a function to be executed ina non-blocking manner
- Basically, program doesn’t wait for the task to complete before moving on to execute the next line of code
- EX use: Web servers & GUI, improves performance & responsiveness.
Thread pool queues
- Crucial component of thread pool design
- Tasks are added to a work queue by application, can be viewed as a shared resource that is associated by the worker threads
How do the thread pool queues improve performance?
- Thread finds available task = picks it up & executes it
- Eliminates need to create & destroy threads continuously
Dynamic & static thread pool queues
- Fixed-size queue: limits nr of tasks that can be queued. Maximum capacity = new tasks rejected until a slot becomes available
- Dynamic queue: Automatically adjusts its size based on the workload
How can thread pool queues ensure load balance?
- Beneficial for load balancing since they help distribute tasks evenly among worker threads
- No thread is overloaded
- Results = improved performance & reduced latency
The downsides of using a single thread for clients’ requests, or a seperate thread for each request?
One thread: Takes too long
New threads: Overhead
How can a thread pool with a limited nr of worker threads be effective in handling requests?
- Request arrives: server adds it to queue of pending requests
- Worker threads periodically check queue for new request. If it exists, it picks it up. When done, returns to the pool.
- Results:
- No running outta system resources
- Improved responsiveness
- No complex thread management for the programmer
Creating a thread pool in C# using a lambda expression function
public void Create()
{
ThreadPool.QueueUserWorkItem((state) =>
{
Console.WriteLine(“A thread pool has been
made”);
});
Disadvantages of thread pools
- Lack of control over threads. Never know when a thread has started, finished, etc. Difficult to debug.
- Limited scalability. Pools have fixed number of threads, not scalable for apps with large number of threads. Too many threads = app crash.
- Potential for thread starvation. Task waits for available thread to execute, but all threads are busy. Delay in task execution = decreased performance.
- Complex programming. Requires careful programming => development process more difficult & time-consuming.
- Increased memory usage. Increase memory usage if nr of threads is too high. Each thread requires own stack & other resources, can quickly add up in a large thread pool.
When to use thread pools?
- Apps that create & destroy many threads
- Apps with many small tasks to be done asynchronously
- Apps that process independent work items in the background & in parallel
When not to use thread pools?
- Apps with tasks having varying execution times
- Apps that require fine-grained control over threads or require specific thread attributes
What is asynchronous programming?
- Type of parallel programming
- Enables unit of work to execute seperately from the main application thread
- Task finished = notifies caller thread, whether successful or failed.
- Provides a nin-block, event-driven model that enhances responsiveness & performance
What is the non-blocking model?
- Can use the same thread to process many tasks, not needing to complete one before moving on to the next
- Improves performance by utilixing thread to execute many tasks concurrently
- (Gas station example)