Skip to content

Commit

Permalink
MonitorPatternTest++
Browse files Browse the repository at this point in the history
MonitorPatternTest++
  • Loading branch information
inbravo committed May 30, 2017
1 parent 2f472c0 commit daa6020
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
- [Usage of 'Timer' and 'TimerTask'][TimerTest.md]
- [Example of failed concurrency][FailedConcurrency.md]
- [Example of successful concurrency][SuccessfullConcurrency.md]
- [Example of Java Monitor Pattern][MonitorPatternTest.md]
- [Example to find number of cores of a processor][AvailableProcessorTest.md]
- [Example of blocking array implemented using 'ReentrantLock'][BlockingArray.md]
- [Example of fork and join pool class 'ForkJoinPool'][ForkJoinPoolTest.md]
Expand Down Expand Up @@ -117,6 +118,7 @@
[SemaphoreTest.md]: https://github.com/inbravo/java-src/blob/master/src/com/inbravo/concurrency/SemaphoreTest.java
[FailedConcurrency.md]: https://github.com/inbravo/java-src/blob/master/src/com/inbravo/concurrency/FailedConcurrency.java
[SuccessfullConcurrency.md]: https://github.com/inbravo/java-src/blob/master/src/com/inbravo/concurrency/SuccessfullConcurrency.java
[MonitorPatternTest.md]: https://github.com/inbravo/java-src/blob/master/src/com/inbravo/concurrency/MonitorPatternTest.java
[TimerTest.md]: https://github.com/inbravo/java-src/blob/master/src/com/inbravo/concurrency/TimerTest.java
[Stack.md]: https://github.com/inbravo/java-src/blob/master/src/com/inbravo/ds.stack/Stack.java
[ReverseTheWord.md]: https://github.com/inbravo/java-src/blob/master/src/com/inbravo/ds/stack/ReverseTheWord.java
Expand Down
114 changes: 114 additions & 0 deletions src/com/inbravo/concurrency/MonitorPatternTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.inbravo.concurrency;

import java.util.concurrent.Semaphore;


/**
*
* @author amit.dixit
*
*/
public final class MonitorPatternTest {

/* Create new Semaphore objects in class */
final private Semaphore firstLock = new Semaphore(1);
final private Semaphore secondLock = new Semaphore(1);

/**
*
* @param args
*/
public static final void main(final String... args) {

/* Create new instance of lock test */
final MonitorPatternTest lockTest = new MonitorPatternTest();

/* Number of threads */
for (int i = 0; i < 1000; i++) {

/* Create first anonymous thread */
new Thread() {

public void run() {

lockTest.threadSafeMethodOne();
}
}.start();

/* Create second anonymous thread */
new Thread() {

public void run() {

lockTest.threadSafeMethodTwo();
}
}.start();
}
}

/**
* First thread is still in critical section and second thread also enters
*
* Output:
*
* First-Thread is inside critical section Second-Thread is inside critical section Second-Thread
* is out of critical section First-Thread is out of critical section
*/
private final void threadSafeMethodOne() {

try {

/* Acquire the lock */
firstLock.acquire();

/* Print current thread info */
System.out.println(Thread.currentThread().getName() + " is inside critical section of object:" + System.identityHashCode(this)
+ "'s method threadSafeMethodOne at time:[" + System.currentTimeMillis() + "]");

/* Sleep this thread so that another thread can do the same operation */
Thread.sleep(1000);

} catch (final InterruptedException e) {
e.printStackTrace();
} finally {

/* Release the lock */
firstLock.release();
System.out.println(Thread.currentThread().getName() + " is out of critical section of object:" + System.identityHashCode(this)
+ "'s method threadSafeMethodOne at time:[" + System.currentTimeMillis() + "]");
}
}

/**
* Second thread only enters critical section only when first thread is out
*
* Output:
*
* First-Thread is inside critical section First-Thread is out of critical section Second-Thread
* is inside critical section Second-Thread is out of critical section
*/
private final void threadSafeMethodTwo() {

try {

/* Acquire the lock */
secondLock.acquire();

/* Print current thread info */
System.out.println(Thread.currentThread().getName() + " is inside critical section of object:" + System.identityHashCode(this)
+ "'s method threadSafeMethodTwo at time:[" + System.currentTimeMillis() + "]");

/* Sleep this thread so that another thread can do the same operation */
Thread.sleep(1000);

} catch (final InterruptedException e) {
e.printStackTrace();
} finally {

/* Release the lock */
secondLock.release();
System.out.println(Thread.currentThread().getName() + " is out of critical section of object:" + System.identityHashCode(this)
+ "'s method threadSafeMethodTwo at time:[" + System.currentTimeMillis() + "]");
}
}
}

0 comments on commit daa6020

Please sign in to comment.