Pattern Design Concurrency Patterns 1 — Questions and Answers
Question 1: What is the primary purpose of the Active Object pattern in concurrent programming?
- To serialize all method calls into a single thread of execution
- To decouple method execution from method invocation for objects that reside in their own thread of control (Correct answer)
- To create a pool of threads that execute tasks on demand
- To synchronize access to shared resources using mutual exclusion locks
Correct answer: To decouple method execution from method invocation for objects that reside in their own thread of control
The Active Object pattern decouples method invocation from execution by running the object in its own thread and using a request queue to buffer calls.
Question 2: Which concurrency pattern ensures that only one thread can execute a critical section at a time by combining mutual exclusion with condition synchronization?
- Observer Pattern
- Half-Sync/Half-Async
- Monitor Object (Correct answer)
- Thread Pool
Correct answer: Monitor Object
The Monitor Object pattern synchronizes concurrent method execution by combining a mutual exclusion lock with condition variables within a single object.
Question 3: The Thread Pool pattern is primarily used to:
- Create a new thread for every incoming request to maximize responsiveness
- Ensure threads execute tasks in a strict priority order
- Share mutable data between threads without requiring synchronization
- Limit the number of concurrent threads and reuse them across multiple tasks (Correct answer)
Correct answer: Limit the number of concurrent threads and reuse them across multiple tasks
Thread Pool limits thread creation overhead by maintaining a fixed set of reusable threads that pick up tasks from a shared work queue.
Question 4: Which pattern uses a shared queue to pass work items between threads that produce data and threads that consume it?
- Singleton
- Producer-Consumer (Correct answer)
- Decorator
- Command
Correct answer: Producer-Consumer
The Producer-Consumer pattern decouples producers and consumers by having them communicate through a bounded or unbounded shared queue.
Question 5: The Read-Write Lock pattern allows:
- Multiple readers simultaneously but only one writer at a time, with writers excluding all readers (Correct answer)
- Only one thread to either read or write at any given time
- Multiple writers simultaneously but only one reader at a time
- Threads to read and write simultaneously without any restriction
Correct answer: Multiple readers simultaneously but only one writer at a time, with writers excluding all readers
A Read-Write Lock grants concurrent access to multiple readers but ensures exclusive access for writers, improving throughput for read-heavy workloads.
Question 6: What is the primary benefit of the Scheduler concurrency pattern?
- It creates new threads dynamically whenever the system load increases
- It increases CPU utilization by running all available threads simultaneously
- It prevents deadlocks by detecting and breaking circular resource dependencies
- It controls the order in which threads access shared resources based on a defined policy (Correct answer)
Correct answer: It controls the order in which threads access shared resources based on a defined policy
The Scheduler pattern centralizes thread-ordering policy, allowing strategies such as priority-based or FIFO access to shared resources.
Question 7: In concurrent programming, what is a Future (also called a Promise)?
- A mechanism used to cancel a running thread immediately
- A lock that prevents multiple threads from accessing a shared resource
- A thread that runs indefinitely in the background processing events
- A placeholder object representing a result that will be available asynchronously (Correct answer)
Correct answer: A placeholder object representing a result that will be available asynchronously
A Future is a proxy for a result not yet computed; callers receive the Future immediately and retrieve the actual value once the async computation completes.
What is the primary purpose of the Active Object pattern in concurrent programming?