Module 5: Threadpools & Asynchronous Programming Flashcards

1
Q

Why can singular threads create an issue?

A
  • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do thread pools work?

A
  • Thread pool manages a pool of worker threads & assigns them to execute the work items/tasks.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are asynchronous calls?

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Thread pool queues

A
  • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do the thread pool queues improve performance?

A
  • Thread finds available task = picks it up & executes it
  • Eliminates need to create & destroy threads continuously
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Dynamic & static thread pool queues

A
  • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can thread pool queues ensure load balance?

A
  • Beneficial for load balancing since they help distribute tasks evenly among worker threads
  • No thread is overloaded
  • Results = improved performance & reduced latency
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

The downsides of using a single thread for clients’ requests, or a seperate thread for each request?

A

One thread: Takes too long
New threads: Overhead

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How can a thread pool with a limited nr of worker threads be effective in handling requests?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Creating a thread pool in C# using a lambda expression function

A

public void Create()
{
ThreadPool.QueueUserWorkItem((state) =>
{
Console.WriteLine(“A thread pool has been
made”);
});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Disadvantages of thread pools

A
  1. Lack of control over threads. Never know when a thread has started, finished, etc. Difficult to debug.
  2. Limited scalability. Pools have fixed number of threads, not scalable for apps with large number of threads. Too many threads = app crash.
  3. Potential for thread starvation. Task waits for available thread to execute, but all threads are busy. Delay in task execution = decreased performance.
  4. Complex programming. Requires careful programming => development process more difficult & time-consuming.
  5. 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

When to use thread pools?

A
  1. Apps that create & destroy many threads
  2. Apps with many small tasks to be done asynchronously
  3. Apps that process independent work items in the background & in parallel
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

When not to use thread pools?

A
  1. Apps with tasks having varying execution times
  2. Apps that require fine-grained control over threads or require specific thread attributes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is asynchronous programming?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the non-blocking model?

A
  • 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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Single-threaded sync code

A
  • 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
17
Q

Multiple-threaded synchronous code

A
  • 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
18
Q

Asynchronous model

A
  • 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.
19
Q

What are some benefits of asynchronous model?

A
  • Programmer has more control over the execution order of tasks
  • greater flexibility in scheduling & prioritizing tasks = improve application performance & responsiveness
20
Q

When should you use single-threaded async code?

A
  • 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
21
Q

Practical example of async

A
  • 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
22
Q

Multi-threaded async code

A
  • Execution of multiple instructions on multiple cores/CPU simultaneously
  • Utilizes potentia of multi-core machines. Allows highly concurrent & true parallelism = super performance!!
23
Q

Async and thread pools?

A
  • 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
24
Q

Defining tasks

A
  • 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
25
Q

Promises/futures

A
  • 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
26
Q

Tasks in C#

A
  • 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