-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLockVsMonitor.java
More file actions
129 lines (117 loc) · 5.19 KB
/
Copy pathLockVsMonitor.java
File metadata and controls
129 lines (117 loc) · 5.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package Phase7_Concurrency.Multithreading;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* Lock vs Monitor — Concurrency Primitives Compared
* -------------------------------------------------
* "Monitor" here means the INTRINSIC LOCK on every Object, accessed via
* `synchronized`, with `wait/notify/notifyAll` as the condition variable.
* <p>
*
* "Lock" means the `java.util.concurrent.locks.Lock` family — typically
* `ReentrantLock` with `Condition` objects.
* <p>
*
* Side-by-side table
* ------------------
* Monitor (synchronized) Lock (j.u.c.locks)
* ----------------------- -------------------
* Acquire synchronized (obj) { ... } lock.lock()
* Release auto on block exit try/finally + lock.unlock()
* Timed acquire no tryLock(time, unit)
* Interruptible acquire no lockInterruptibly()
* Fair acquire no new ReentrantLock(true)
* Reentrant yes yes (ReentrantLock)
* Condition variables wait/notify(All) — one set newCondition() × N
* Performance great after biased lock removal; depends on JVM
* comparable; sometimes faster
* Read/write split? no ReentrantReadWriteLock
* Optimistic read? no StampedLock
* <p>
*
* When to use which
* -----------------
* - SMALL, OBVIOUS critical section → synchronized. Less to type, less
* to forget.
* - Need ANY of: timed acquire, interruptible acquire, fairness,
* multiple conditions, read/write separation → Lock family.
* - "Drop-in" parity: ReentrantLock matches synchronized semantics
* including reentrancy.
* <p>
*
* Don't mix them
* --------------
* Guarding the same field with BOTH synchronized and a Lock is a recipe
* for confusion and lock-ordering bugs. Pick one mechanism per field.
*/
public class LockVsMonitor {
/** Same counter, two implementations. */
static class MonitorCounter {
private int n;
public synchronized void bump() { n++; }
public synchronized int get() { return n; }
}
static class LockCounter {
private final Lock lock = new ReentrantLock();
private int n;
public void bump() {
lock.lock();
try { n++; }
finally { lock.unlock(); }
}
public int get() {
lock.lock();
try { return n; }
finally { lock.unlock(); }
}
}
public static void main(String[] args) throws InterruptedException {
section("1) Equivalent behaviour — same correct count");
MonitorCounter mc = new MonitorCounter();
LockCounter lc = new LockCounter();
Runnable bumpM = () -> { for (int i = 0; i < 100_000; i++) mc.bump(); };
Runnable bumpL = () -> { for (int i = 0; i < 100_000; i++) lc.bump(); };
Thread tm1 = new Thread(bumpM); Thread tm2 = new Thread(bumpM);
Thread tl1 = new Thread(bumpL); Thread tl2 = new Thread(bumpL);
tm1.start(); tm2.start(); tl1.start(); tl2.start();
tm1.join(); tm2.join(); tl1.join(); tl2.join();
System.out.println("Monitor counter = " + mc.get());
System.out.println("Lock counter = " + lc.get());
section("2) Lock CAN time out; monitor cannot");
Lock lock = new ReentrantLock();
Thread holder = new Thread(() -> {
lock.lock();
try { Thread.sleep(200); } catch (InterruptedException ignored) {}
finally { lock.unlock(); }
}, "holder");
holder.start();
Thread.sleep(30); // ensure holder grabs the lock
boolean got = lock.tryLock(50, TimeUnit.MILLISECONDS);
System.out.println("tryLock(50ms) = " + got);
if (got) lock.unlock();
holder.join();
section("3) Multiple Conditions on one Lock");
Lock l = new ReentrantLock();
Condition condA = l.newCondition();
Condition condB = l.newCondition();
// With a monitor you can only have ONE condition (the object itself).
// With a Lock you can have as many as you like.
System.out.println("created " + condA + " and " + condB);
section("4) DON'T mix monitor + Lock on the same field");
// Imagine the developer who would write this:
//
// synchronized (this) { this.value = 42; }
// // ... in another method ...
// lock.lock(); try { this.value++; } finally { lock.unlock(); }
//
// Now `value` is guarded by two different things. There is no
// mutual exclusion between the two paths — race city. Pick ONE.
section("done");
}
private static void section(String title) {
System.out.println();
System.out.println("=== " + title + " ===");
}
}