|
| 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