Skip to content

Commit daa6020

Browse files
committed
MonitorPatternTest++
MonitorPatternTest++
1 parent 2f472c0 commit daa6020

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
- [Usage of 'Timer' and 'TimerTask'][TimerTest.md]
5050
- [Example of failed concurrency][FailedConcurrency.md]
5151
- [Example of successful concurrency][SuccessfullConcurrency.md]
52+
- [Example of Java Monitor Pattern][MonitorPatternTest.md]
5253
- [Example to find number of cores of a processor][AvailableProcessorTest.md]
5354
- [Example of blocking array implemented using 'ReentrantLock'][BlockingArray.md]
5455
- [Example of fork and join pool class 'ForkJoinPool'][ForkJoinPoolTest.md]
@@ -117,6 +118,7 @@
117118
[SemaphoreTest.md]: https://github.com/inbravo/java-src/blob/master/src/com/inbravo/concurrency/SemaphoreTest.java
118119
[FailedConcurrency.md]: https://github.com/inbravo/java-src/blob/master/src/com/inbravo/concurrency/FailedConcurrency.java
119120
[SuccessfullConcurrency.md]: https://github.com/inbravo/java-src/blob/master/src/com/inbravo/concurrency/SuccessfullConcurrency.java
121+
[MonitorPatternTest.md]: https://github.com/inbravo/java-src/blob/master/src/com/inbravo/concurrency/MonitorPatternTest.java
120122
[TimerTest.md]: https://github.com/inbravo/java-src/blob/master/src/com/inbravo/concurrency/TimerTest.java
121123
[Stack.md]: https://github.com/inbravo/java-src/blob/master/src/com/inbravo/ds.stack/Stack.java
122124
[ReverseTheWord.md]: https://github.com/inbravo/java-src/blob/master/src/com/inbravo/ds/stack/ReverseTheWord.java
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.inbravo.concurrency;
2+
3+
import java.util.concurrent.Semaphore;
4+
5+
6+
/**
7+
*
8+
* @author amit.dixit
9+
*
10+
*/
11+
public final class MonitorPatternTest {
12+
13+
/* Create new Semaphore objects in class */
14+
final private Semaphore firstLock = new Semaphore(1);
15+
final private Semaphore secondLock = new Semaphore(1);
16+
17+
/**
18+
*
19+
* @param args
20+
*/
21+
public static final void main(final String... args) {
22+
23+
/* Create new instance of lock test */
24+
final MonitorPatternTest lockTest = new MonitorPatternTest();
25+
26+
/* Number of threads */
27+
for (int i = 0; i < 1000; i++) {
28+
29+
/* Create first anonymous thread */
30+
new Thread() {
31+
32+
public void run() {
33+
34+
lockTest.threadSafeMethodOne();
35+
}
36+
}.start();
37+
38+
/* Create second anonymous thread */
39+
new Thread() {
40+
41+
public void run() {
42+
43+
lockTest.threadSafeMethodTwo();
44+
}
45+
}.start();
46+
}
47+
}
48+
49+
/**
50+
* First thread is still in critical section and second thread also enters
51+
*
52+
* Output:
53+
*
54+
* First-Thread is inside critical section Second-Thread is inside critical section Second-Thread
55+
* is out of critical section First-Thread is out of critical section
56+
*/
57+
private final void threadSafeMethodOne() {
58+
59+
try {
60+
61+
/* Acquire the lock */
62+
firstLock.acquire();
63+
64+
/* Print current thread info */
65+
System.out.println(Thread.currentThread().getName() + " is inside critical section of object:" + System.identityHashCode(this)
66+
+ "'s method threadSafeMethodOne at time:[" + System.currentTimeMillis() + "]");
67+
68+
/* Sleep this thread so that another thread can do the same operation */
69+
Thread.sleep(1000);
70+
71+
} catch (final InterruptedException e) {
72+
e.printStackTrace();
73+
} finally {
74+
75+
/* Release the lock */
76+
firstLock.release();
77+
System.out.println(Thread.currentThread().getName() + " is out of critical section of object:" + System.identityHashCode(this)
78+
+ "'s method threadSafeMethodOne at time:[" + System.currentTimeMillis() + "]");
79+
}
80+
}
81+
82+
/**
83+
* Second thread only enters critical section only when first thread is out
84+
*
85+
* Output:
86+
*
87+
* First-Thread is inside critical section First-Thread is out of critical section Second-Thread
88+
* is inside critical section Second-Thread is out of critical section
89+
*/
90+
private final void threadSafeMethodTwo() {
91+
92+
try {
93+
94+
/* Acquire the lock */
95+
secondLock.acquire();
96+
97+
/* Print current thread info */
98+
System.out.println(Thread.currentThread().getName() + " is inside critical section of object:" + System.identityHashCode(this)
99+
+ "'s method threadSafeMethodTwo at time:[" + System.currentTimeMillis() + "]");
100+
101+
/* Sleep this thread so that another thread can do the same operation */
102+
Thread.sleep(1000);
103+
104+
} catch (final InterruptedException e) {
105+
e.printStackTrace();
106+
} finally {
107+
108+
/* Release the lock */
109+
secondLock.release();
110+
System.out.println(Thread.currentThread().getName() + " is out of critical section of object:" + System.identityHashCode(this)
111+
+ "'s method threadSafeMethodTwo at time:[" + System.currentTimeMillis() + "]");
112+
}
113+
}
114+
}

0 commit comments

Comments
 (0)