Pattern Design Concurrency Patterns 2 — Questions and Answers
Question 1: The Half-Sync/Half-Async pattern separates:
- Read and write operations into different dedicated threads
- Producers from consumers using a message-passing pipeline
- Synchronous and asynchronous processing into distinct architectural layers (Correct answer)
- UI rendering threads from background computation threads
Correct answer: Synchronous and asynchronous processing into distinct architectural layers
Half-Sync/Half-Async divides processing into an asynchronous layer (for I/O events) and a synchronous layer (for business logic), connected by a queue.
Question 2: Which concurrency construct is used to block a set of threads until all of them have reached a common synchronization point before any can continue?
- Singleton
- Factory Method
- Observer
- Barrier (Correct answer)
Correct answer: Barrier
A Barrier (or CountDownLatch) holds all participating threads at a meeting point until the last thread arrives, then releases them all simultaneously.
Question 3: The Reactor pattern is most commonly used for:
- Creating objects without specifying their concrete class at compile time
- Event-driven I/O handling that multiplexes and dispatches service requests from concurrent connections (Correct answer)
- Assigning dynamic thread priorities based on system load
- Pooling and reusing database connections across application threads
Correct answer: Event-driven I/O handling that multiplexes and dispatches service requests from concurrent connections
The Reactor pattern demultiplexes I/O events from multiple clients and dispatches them synchronously to registered handlers, enabling scalable single-threaded concurrency.
Question 4: In the Thread-Specific Storage (Thread-Local Storage) pattern, each thread has:
- A dedicated CPU core assigned to it by the operating system
- A shared lock it must acquire before reading shared memory
- Its own independent copy of a variable, invisible to other threads (Correct answer)
- A private event queue for dispatching incoming work items
Correct answer: Its own independent copy of a variable, invisible to other threads
Thread-Specific Storage gives each thread its own private copy of a variable so threads never contend over or interfere with each other's data.
Question 5: Which concurrency pattern is specifically designed to ensure a lazily initialized shared resource is created exactly once in a multithreaded environment while minimizing locking overhead?
- Producer-Consumer
- Thread Pool
- Active Object
- Double-Checked Locking (Correct answer)
Correct answer: Double-Checked Locking
Double-Checked Locking avoids acquiring a lock on every access by first checking whether the resource is already initialized before entering the synchronized block.
Question 6: The Balking pattern causes an object to:
- Block the calling thread until a prerequisite condition becomes true
- Reject (balk at) an operation outright if the object is not in an appropriate state to handle it (Correct answer)
- Distribute pending work items evenly across a pool of worker threads
- Notify other threads via a signal when an asynchronous task completes
Correct answer: Reject (balk at) an operation outright if the object is not in an appropriate state to handle it
With the Balking pattern, an object silently refuses an operation (returns immediately or throws) when its current state makes the operation meaningless or unsafe.
Question 7: What problem does the Guarded Suspension pattern solve?
- It prevents race conditions by temporarily disabling thread scheduling interrupts
- It ensures that only one instance of a class is created across all threads
- It governs memory allocation strategies in multithreaded applications
- It suspends the calling thread until a necessary precondition is true before allowing an operation to proceed (Correct answer)
Correct answer: It suspends the calling thread until a necessary precondition is true before allowing an operation to proceed
Guarded Suspension blocks the caller and releases the lock while waiting; when another thread changes the condition, the suspended thread is notified and re-checks before continuing.
The Half-Sync/Half-Async pattern separates: