mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-21 02:24:21 +00:00
6cd2d0353a
* update yaml frontmatter format * update abstract document * update abstract factory * use the new pattern template * acyclic visitor seo * adapter seo * ambassador seo * acl seo * aaa seo * async method invocation seo * balking seo * bridge seo * builder seo * business delegate and bytecode seo * caching seo * callback seo * chain seo * update headings * circuit breaker seo * client session + collecting parameter seo * collection pipeline seo * combinator SEO * command seo * cqrs seo * commander seo * component seo * composite seo * composite entity seo * composite view seo * context object seo * converter seo * crtp seo * currying seo * dao seo * data bus seo * data locality seo * data mapper seo * dto seo * decorator seo * delegation seo * di seo * dirty flag seo * domain model seo * double buffer seo * double checked locking seo * double dispatch seo * dynamic proxy seo * event aggregator seo * event-based asynchronous seo * eda seo * event queue seo * event sourcing seo * execute around seo * extension objects seo * facade seo * factory seo * factory kit seo * factory method seo * fanout/fanin seo * feature toggle seo * filterer seo * fluent interface seo * flux seo * flyweight seo * front controller seo * function composition seo * game loop seo * gateway seo * guarded suspension seo * half-sync/half-async seo * health check seo * hexagonal seo * identity map seo * intercepting filter seo * interpreter seo * iterator seo * layers seo * lazy loading seo * leader election seo * leader/followers seo * lockable object seo * rename and add seo for marker interface * master-worker seo * mediator seo * memento seo * metadata mapping seo * microservice aggregator seo * api gw seo * microservices log aggregration seo * mvc seo * mvi seo * mvp seo * mvvm seo * monad seo * monitor seo * monostate seo * multiton seo * mute idiom seo * naked objects & notification seo * null object seo * object mother seo * object pool seo * observer seo * optimistic locking seo * page controller seo * page object seo * parameter object seo * partial response seo * pipeline seo * poison pill seo * presentation model seo * private class data seo * producer-consumer seo * promise seo * property seo * prototype seo * proxy seo * queue-based load leveling seo * reactor seo * registry seo * repository seo * RAII seo * retry seo * role object seo * saga seo * separated interface seo * serialized entity seo * serialized lob seo * servant seo * server session seo * service layer seo * service locator seo * service to worker seo * sharding seo * single table inheritance seo * singleton seo * spatial partition seo * special case seo * specification seo * state seo * step builder seo * strangler seo * strategy seo * subclass sandbox seo * table module seo * template method seo * throttling seo * tolerant reader seo * trampoline seo * transaction script seo * twin seo * type object seo * unit of work seo * update method seo * value object seo * version number seo * virtual proxy seo * visitor seo * seo enhancements * seo improvements * SEO enhancements * SEO improvements * SEO additions * SEO improvements * more SEO improvements * rename hexagonal + SEO improvements * SEO improvements * more SEO stuff * SEO improvements * SEO optimizations * SEO enhancements * enchance SEO * improve SEO * SEO improvements * update headers
159 lines
7.5 KiB
Markdown
159 lines
7.5 KiB
Markdown
---
|
|
title: "Guarded Suspension Pattern in Java: Ensuring Safe Concurrency in Critical Sections"
|
|
shortTitle: Guarded Suspension
|
|
description: "Learn about the Guarded Suspension design pattern in Java. Understand its implementation for efficient concurrency control, with real-world examples and code snippets."
|
|
category: Concurrency
|
|
language: en
|
|
tag:
|
|
- Asynchronous
|
|
- Decoupling
|
|
- Resource management
|
|
- Synchronization
|
|
- Thread management
|
|
---
|
|
|
|
## Also known as
|
|
|
|
* Conditional Block
|
|
* Suspended Execution
|
|
|
|
## Intent of Guarded Suspension Design Pattern
|
|
|
|
The Guarded Suspension pattern is crucial in Java design patterns for managing operations that require both a lock and a condition to proceed. It optimizes concurrency control by allowing a thread to wait for the right condition efficiently.
|
|
|
|
## Detailed Explanation of Guarded Suspension Pattern with Real-World Examples
|
|
|
|
Real-world example
|
|
|
|
> A practical example of the Guarded Suspension pattern can be seen in a ride-sharing service. In this system, passengers wait for a car to become available, ensuring efficient resource use without continuous checking. When a passenger requests a ride, the request is suspended until a driver becomes available. The system monitors the availability of drivers, and once a driver is ready to take a new passenger, the system notifies the waiting passenger and resumes the ride request process. This ensures that passengers are not continuously checking for available drivers and that drivers are efficiently matched with passengers based on their availability.
|
|
|
|
In plain words
|
|
|
|
> Guarded Suspension pattern is used when one thread waits for the result of another thread's execution.
|
|
|
|
Wikipedia says
|
|
|
|
> In concurrent programming, Guarded Suspension manages operations requiring a lock and a precondition, delaying execution until the precondition is met.
|
|
|
|
## Programmatic Example of Guarded Suspension Pattern in Java
|
|
|
|
The `GuardedQueue` class in Java showcases concurrent programming using the Guarded Suspension pattern. It includes synchronized methods that manage thread management and synchronization, demonstrating how threads wait for the right conditions to execute.
|
|
|
|
The `GuardedQueue` class demonstrates the Guarded Suspension pattern by encapsulating a queue and providing two synchronized methods, `get` and `put`. The `get` method waits if the queue is empty, while the `put` method adds an item to the queue and notifies any waiting threads.
|
|
|
|
```java
|
|
@Slf4j
|
|
public class GuardedQueue {
|
|
private final Queue<Integer> sourceList = new LinkedList<>();
|
|
|
|
// Synchronized get method waits until the queue is not empty
|
|
public synchronized Integer get() {
|
|
while (sourceList.isEmpty()) {
|
|
try {
|
|
wait();
|
|
} catch (InterruptedException e) {
|
|
LOGGER.error("Error occurred: ", e);
|
|
}
|
|
}
|
|
return sourceList.poll();
|
|
}
|
|
|
|
// Synchronized put method adds an item to the queue and notifies waiting threads
|
|
public synchronized void put(Integer e) {
|
|
sourceList.add(e);
|
|
notify();
|
|
}
|
|
}
|
|
```
|
|
|
|
* `get`: This method waits while the `sourceList` is empty. When an item is added and `notify` is called, the waiting thread is awakened to continue execution and retrieve the item.
|
|
* `put`: This method adds an item to the queue and calls `notify` to wake up any waiting thread that is suspended in the `get` method.
|
|
|
|
Here is the `App` class driving the example:
|
|
|
|
```java
|
|
public class App {
|
|
public static void main(String[] args) {
|
|
GuardedQueue guardedQueue = new GuardedQueue();
|
|
ExecutorService executorService = Executors.newFixedThreadPool(3);
|
|
|
|
// Thread to get from the guardedQueue
|
|
executorService.execute(() -> {
|
|
Integer item = guardedQueue.get();
|
|
LOGGER.info("Retrieved: " + item);
|
|
});
|
|
|
|
// Simulating some delay before putting an item
|
|
try {
|
|
Thread.sleep(2000);
|
|
} catch (InterruptedException e) {
|
|
LOGGER.error("Error occurred: ", e);
|
|
}
|
|
|
|
// Thread to put an item into the guardedQueue
|
|
executorService.execute(() -> {
|
|
guardedQueue.put(20);
|
|
LOGGER.info("Item added to queue");
|
|
});
|
|
|
|
executorService.shutdown();
|
|
try {
|
|
executorService.awaitTermination(30, TimeUnit.SECONDS);
|
|
} catch (InterruptedException e) {
|
|
LOGGER.error("Error occurred: ", e);
|
|
}
|
|
}
|
|
}
|
|
```
|
|
|
|
* `ExecutorService` is used to manage a pool of threads.
|
|
* The first thread attempts to retrieve an item from the `GuardedQueue` and waits since the queue is initially empty.
|
|
* After a 2-second delay, the second thread adds an item to the queue, waking up the first thread.
|
|
* The first thread then retrieves the item and logs it.
|
|
|
|
Execution yields:
|
|
|
|
```
|
|
19:22:58.984 [pool-1-thread-1] INFO com.iluwatar.guarded.suspension.GuardedQueue -- waiting
|
|
19:23:00.993 [pool-1-thread-2] INFO com.iluwatar.guarded.suspension.GuardedQueue -- putting
|
|
19:23:00.994 [pool-1-thread-2] INFO com.iluwatar.guarded.suspension.GuardedQueue -- notifying
|
|
19:23:00.994 [pool-1-thread-1] INFO com.iluwatar.guarded.suspension.GuardedQueue -- getting
|
|
19:23:00.994 [pool-1-thread-1] INFO com.iluwatar.guarded.suspension.GuardedQueue -- Retrieved: 20
|
|
```
|
|
|
|
* The log output shows the sequence of events: the first thread waits, the second thread puts an item, and the first thread then retrieves the item. This demonstrates the Guarded Suspension pattern in action.
|
|
|
|
## When to Use the Guarded Suspension Pattern in Java
|
|
|
|
This pattern is ideal for scenarios requiring a thread to wait for specific conditions, promoting efficient concurrency control and reducing busy waiting overhead.
|
|
|
|
## Real-World Applications of Guarded Suspension Pattern in Java
|
|
|
|
* Network servers waiting for client requests.
|
|
* Producer-consumer scenarios where the consumer must wait for the producer to provide data.
|
|
* Event-driven applications where actions are triggered only after specific events have occurred.
|
|
|
|
## Benefits and Trade-offs of Guarded Suspension Pattern
|
|
|
|
Benefits:
|
|
|
|
* Reduces CPU consumption by preventing busy waiting.
|
|
* Increases system responsiveness by synchronizing actions to the availability of necessary conditions or resources.
|
|
|
|
Trade-offs:
|
|
|
|
* Complexity in implementation, especially when multiple conditions need to be managed.
|
|
* Potential for deadlocks if not carefully managed.
|
|
|
|
## Related Java Design Patterns
|
|
|
|
* [Monitor](https://java-design-patterns.com/patterns/monitor/): Both patterns manage the synchronization of threads based on conditions. Guarded Suspension specifically deals with suspending threads until conditions are met, while Monitor Object encapsulates condition and mutual exclusion handling.
|
|
* [Producer-Consumer](https://java-design-patterns.com/patterns/producer-consumer/): Often implemented using Guarded Suspension to handle waiting consumers and producers efficiently.
|
|
* [Balking](https://java-design-patterns.com/patterns/balking/): Similar to Guarded Suspension, Balking is used when a thread checks a condition and only proceeds if the condition is favorable; if not, it immediately returns or bails out. This pattern complements Guarded Suspension by managing actions based on immediate condition checks without waiting.
|
|
|
|
## References and Credits
|
|
|
|
* [Concurrent Programming in Java : Design Principles and Patterns](https://amzn.to/4dIBqxL)
|
|
* [Java Concurrency in Practice](https://amzn.to/3JxnXek)
|
|
* [Pattern-Oriented Software Architecture Volume 2: Patterns for Concurrent and Networked Objects](https://amzn.to/49Ke1c9)
|