You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* add monitor design pattern .
* add extra line and change compiler version to 11 in pom.xml.
* encapsulate getBalance method .
* update puml file .
* export uml as png .
* duplicate codes eliminated .
* update tag
* change the format of pom.xml
* using logger to print
* change AtomicRefrence to type inference var
* explanations added !
* Update monitor/README.md
Co-authored-by: Ilkka Seppälä <iluwatar@users.noreply.github.com>
* Update monitor/README.md
Co-authored-by: Ilkka Seppälä <iluwatar@users.noreply.github.com>
* Update monitor/src/main/java/com/iluwatar/monitor/Main.java
Co-authored-by: Ilkka Seppälä <iluwatar@users.noreply.github.com>
* Update monitor/src/main/java/com/iluwatar/monitor/Main.java
Co-authored-by: Ilkka Seppälä <iluwatar@users.noreply.github.com>
* Update monitor/src/main/java/com/iluwatar/monitor/Main.java
Co-authored-by: Ilkka Seppälä <iluwatar@users.noreply.github.com>
* Update monitor/src/main/java/com/iluwatar/monitor/Main.java
Co-authored-by: Ilkka Seppälä <iluwatar@users.noreply.github.com>
* e.printStackTrace have changed to logger to prints standard output (STD OUT) .
* add programmatic example .
* Delete mvnw
* mvnw.cmd deleted .
* added mvnw from master
* AddUnitTest
* Add language to readme.md
Co-authored-by: Subhrodip Mohanta <subhrodipmohanta@gmail.com>
Co-authored-by: Ilkka Seppälä <iluwatar@users.noreply.github.com>
Co-authored-by: Subhrodip Mohanta <subhromo@cisco.com>
Co-authored-by: Subhrodip Mohanta <contact@subho.xyz>
Monitor pattern is used to create thread-safe objects and prevent conflicts between threads in concurrent applications.
14
+
15
+
## Explanation
16
+
17
+
In plain words
18
+
19
+
> Monitor pattern is used to enforce single-threaded access to data. Only one thread at a time is allowed to execute code within the monitor object.
20
+
21
+
Wikipedia says
22
+
23
+
> 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.
24
+
25
+
**Programmatic Examples**
26
+
27
+
Consider there is a bank that transfers money from an account to another account with transfer method . it is `synchronized` mean just one thread can access to this method because if many threads access to it and transfer money from an account to another account in same time balance changed !
28
+
29
+
```
30
+
class Bank {
31
+
32
+
private int[] accounts;
33
+
Logger logger;
34
+
35
+
public Bank(int accountNum, int baseAmount, Logger logger) {
36
+
this.logger = logger;
37
+
accounts = new int[accountNum];
38
+
Arrays.fill(accounts, baseAmount);
39
+
}
40
+
41
+
public synchronized void transfer(int accountA, int accountB, int amount) {
0 commit comments