mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-24 06:25:27 +00:00
docs: Content SEO updates (#2990)
* 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
This commit is contained in:
+19
-18
@@ -1,24 +1,26 @@
|
||||
---
|
||||
title: Monitor
|
||||
title: "Monitor Pattern in Java: Implementing Robust Locking Mechanisms with Monitors"
|
||||
shortTitle: Monitor
|
||||
description: "Learn how the Monitor design pattern in Java ensures thread safety and synchronization. Explore examples, applicability, and benefits of using monitors in concurrent programming."
|
||||
category: Concurrency
|
||||
language: en
|
||||
tag:
|
||||
- Encapsulation
|
||||
- Fault tolerance
|
||||
- Isolation
|
||||
- Synchronization
|
||||
- Thread management
|
||||
- Encapsulation
|
||||
- Fault tolerance
|
||||
- Isolation
|
||||
- Synchronization
|
||||
- Thread management
|
||||
---
|
||||
|
||||
## Also known as
|
||||
|
||||
* Synchronized Block
|
||||
|
||||
## Intent
|
||||
## Intent of Monitor Design Pattern
|
||||
|
||||
The Monitor design pattern is used to synchronize concurrent operations by encapsulating shared resources in such a way that only one thread can access them at a time, ensuring thread safety.
|
||||
The Monitor design pattern in Java is essential for synchronizing concurrent operations, ensuring thread safety and preventing race conditions.
|
||||
|
||||
## Explanation
|
||||
## Detailed Explanation of Monitor Pattern with Real-World Examples
|
||||
|
||||
Real-world example
|
||||
|
||||
@@ -34,11 +36,11 @@ Wikipedia says
|
||||
|
||||
> In concurrent programming (also known as parallel programming), a monitor is a synchronization construct that allows threads to have both mutual exclusion and the ability to wait (block) for a certain condition to become false. Monitors also have a mechanism for signaling other threads that their condition has been met.
|
||||
|
||||
**Programmatic Examples**
|
||||
## Programmatic Example of Monitor Pattern in Java
|
||||
|
||||
The Monitor design pattern is a synchronization technique used in concurrent programming to ensure that only one thread can execute a particular section of code at a time. It is a method of wrapping and hiding the synchronization primitives (like semaphores or locks) within the methods of an object. This pattern is useful in situations where race conditions could occur.
|
||||
|
||||
In the provided code, the `Bank` class is an example of the Monitor pattern. The `Bank` class has several methods that are marked as `synchronized`. This means that only one thread can execute these methods at a time, ensuring that the bank's state remains consistent even when accessed from multiple threads.
|
||||
The Java Monitor design pattern can be seen in the `Bank` class example. By using synchronized methods, the `Bank` class ensures that only one thread can perform transactions at any given time, illustrating effective use of the Monitor pattern in real-world applications.
|
||||
|
||||
Here is a simplified version of the `Bank` class with additional comments:
|
||||
|
||||
@@ -115,7 +117,7 @@ public class Main {
|
||||
|
||||
In this example, the `Bank` class is the monitor, and the `transfer` method is the critical section that needs to be executed in a mutually exclusive manner. The `synchronized` keyword in Java is used to implement the Monitor pattern, ensuring that only one thread can execute the `transfer` method at a time.
|
||||
|
||||
## Applicability
|
||||
## When to Use the Monitor Pattern in Java
|
||||
|
||||
The Monitor design pattern should be used in situations where you have shared resources that need to be accessed and manipulated by multiple threads or processes concurrently. This pattern is particularly useful in scenarios where synchronization is necessary to prevent race conditions, data corruption, and inconsistent states. Here are some situations where you should consider using the Monitor pattern:
|
||||
|
||||
@@ -139,12 +141,11 @@ However, it's important to note that the Monitor pattern might not be the best f
|
||||
|
||||
Before applying the Monitor pattern, it's recommended to thoroughly analyze your application's concurrency requirements and choose the synchronization approach that best suits your needs, taking into consideration factors like performance, complexity, and available language features.
|
||||
|
||||
## Known Uses
|
||||
## Real-World Applications of Monitor Pattern in Java
|
||||
|
||||
* Java's synchronized methods and blocks.
|
||||
* Implementations of concurrent data structures like Vector and Hashtable in the Java Collections Framework.
|
||||
Common implementations of the Monitor design pattern in Java include synchronized methods and blocks, as well as concurrent data structures like `Vector` and `Hashtable`.
|
||||
|
||||
## Consequences
|
||||
## Benefits and Trade-offs of Monitor Pattern
|
||||
|
||||
Benefits:
|
||||
|
||||
@@ -156,12 +157,12 @@ Trade-offs:
|
||||
* Can lead to decreased performance due to locking overhead.
|
||||
* Potential for deadlocks if not carefully designed.
|
||||
|
||||
## Related Patterns
|
||||
## Related Java Design Patterns
|
||||
|
||||
Semaphore: Used to control access to a common resource by multiple threads; Monitor uses a binary semaphore concept at its core.
|
||||
Mutex: Another mechanism for ensuring mutual exclusion; Monitor is a higher-level construct often implemented using mutexes.
|
||||
|
||||
## Credits
|
||||
## References and Credits
|
||||
|
||||
* [Concurrency: State Models & Java Programs](https://amzn.to/4dxxjUX)
|
||||
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
|
||||
|
||||
Reference in New Issue
Block a user