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)
Single-threaded sync code
- Sync sequential code = executes in deterministic order, produce the same results for the same input everytime.
- Programmer has complete control over operations
- Easy to read, straightforward BUT… =>
- May be slow = unresponsive app
Multiple-threaded synchronous code
- Each task executes on a seperate thread => tasks run concurrently or in parallel
- Less control over execution order of tasks
- Requires effective communication & coordination between threads
Asynchronous model
- Allows tasks to execute independently & concurrently, without blocking main thread of application
- Not necessarily have seperate threads for each task. Tasks can be managed by a single thread or small pool of threads.
What are some benefits of asynchronous model?
- Programmer has more control over the execution order of tasks
- greater flexibility in scheduling & prioritizing tasks = improve application performance & responsiveness
When should you use single-threaded async code?
- Apps with large number of tasks, esp that require little communication btw tasks
- Only blocks when no task can make progress = referred to as non-blocking program. Reducing waiting time = improve app performance.
- Good resource utilization & efficiency = reduces context switching & minimizes memory consumption
Practical example of async
- Graphical user interface (GUI) needs to be responsive
- Too many threads = decrease responsiveness. Not good for user experience.
- Async with a single thread = GUI maintain high level of responsiveness = better user experience
Multi-threaded async code
- Execution of multiple instructions on multiple cores/CPU simultaneously
- Utilizes potentia of multi-core machines. Allows highly concurrent & true parallelism = super performance!!
Async and thread pools?
- Thread pools used in async programming for efficient use of system resources
- Allows overhead of creating & managing many threads
- Useful for apps with short-lived tasks
Defining tasks
- Task in async programming = unit of work that needs to be executed asynchronously.
- Can represent any operation that needs to be executed, eg reading data to a file
Promises/futures
- Tasks return a promise or a future object
- Caller of the method can use this object to track the progress of the task & retrive its result when completed
Tasks in C#
- Task Class
- async = marks method as asynchronous, perform work without blocking the calling thread
- await keywords = wait for completion of other tasks
- Task.WhenAll = waits for all provided tasks to complete
- Task.WhenAny = waits for the first task to complete
- CancellationToken class = cancel asynchronous operation before completion