Skip to content

Commit 70692f6

Browse files
authored
docs: readme improvements (iluwatar#2572)
1 parent 0fdb111 commit 70692f6

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

Diff for: monitor/README.md

+42-5
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,15 @@ tag:
66
- Performance
77
---
88

9+
## Also known as
10+
11+
Monitor object pattern
12+
913
## Intent
10-
Monitor pattern is used to create thread-safe objects and prevent conflicts between threads in concurrent applications.
14+
15+
The primary intent is to provide a structured and controlled way for multiple threads or processes to safely access and
16+
manipulate shared resources, such as variables, data structures, or critical sections of code, without causing conflicts
17+
or race conditions.
1118

1219
## Explanation
1320

@@ -61,8 +68,38 @@ getBalance always return total amount and the total amount should be same after
6168
![alt text](./etc/monitor.urm.png "Monitor class diagram")
6269

6370
## Applicability
64-
Use the Monitor pattern when
6571

66-
* we have a shared resource and there is critical section .
67-
* you want to create thread-safe objects .
68-
* you want to achieve mutual exclusion in high level programming language .
72+
The Monitor design pattern should be used in situations where you have shared resources that need to be accessed and
73+
manipulated by multiple threads or processes concurrently. This pattern is particularly useful in scenarios where
74+
synchronization is necessary to prevent race conditions, data corruption, and inconsistent states. Here are some
75+
situations where you should consider using the Monitor pattern:
76+
77+
1. **Shared Data**: When your application involves shared data structures, variables, or resources that need to be accessed and updated by multiple threads. Monitors ensure that only one thread can access the shared resource at a time, preventing conflicts and ensuring data consistency.
78+
79+
2. **Critical Sections**: When you have critical sections of code that need to be executed by only one thread at a time. Critical sections are portions of code where shared resources are manipulated, and concurrent access could lead to problems. Monitors help ensure that only one thread can execute the critical section at any given time.
80+
81+
3. **Thread Safety**: When you need to ensure thread safety without relying solely on low-level synchronization mechanisms like locks and semaphores. Monitors provide a higher-level abstraction that encapsulates synchronization and resource management.
82+
83+
4. **Waiting and Signaling**: When you have scenarios where threads need to wait for certain conditions to be met before proceeding. Monitors often include mechanisms for threads to wait for specific conditions and for other threads to notify them when the conditions are satisfied.
84+
85+
5. **Deadlock Prevention**: When you want to prevent deadlocks by providing a structured way to acquire and release locks on shared resources. Monitors help avoid common deadlock scenarios by ensuring that resource access is well-managed.
86+
87+
6. **Concurrent Data Structures**: When you're implementing concurrent data structures, such as queues, stacks, or hash tables, where multiple threads need to manipulate the structure while maintaining its integrity.
88+
89+
7. **Resource Sharing**: When multiple threads need to share limited resources, like connections to a database or access to a network socket. Monitors can help manage the allocation and release of these resources in a controlled manner.
90+
91+
8. **Improved Maintainability**: When you want to encapsulate synchronization logic and shared resource management within a single object, improving code organization and making it easier to reason about concurrency-related code.
92+
93+
However, it's important to note that the Monitor pattern might not be the best fit for all concurrency scenarios. In
94+
some cases, other synchronization mechanisms like locks, semaphores, or concurrent data structures might be more
95+
suitable. Additionally, modern programming languages and frameworks often provide higher-level concurrency constructs
96+
that abstract away the complexities of low-level synchronization.
97+
98+
Before applying the Monitor pattern, it's recommended to thoroughly analyze your application's concurrency requirements
99+
and choose the synchronization approach that best suits your needs, taking into consideration factors like performance,
100+
complexity, and available language features.
101+
102+
## Related patterns
103+
104+
* Active object
105+
* Double-checked locking

Diff for: monitor/src/test/java/com/iluwatar/monitor/MainTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void shouldExecuteApplicationWithoutException() {
3737
}
3838

3939
@Test
40-
void RunnerExecuteWithoutException() {
40+
void runnerShouldExecuteWithoutException() {
4141
var bank = new Bank(4, 1000);
4242
var latch = new CountDownLatch(1);
4343

0 commit comments

Comments
 (0)