-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLockInterface.java
More file actions
152 lines (108 loc) · 3.37 KB
/
LockInterface.java
File metadata and controls
152 lines (108 loc) · 3.37 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* 4. Lock */
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/*
Synchronization achieves :
1. Mutual Exclusion
MutEx
2. Visibility
value is read from memory before block execution
value is written to memory after block execution
Thread local :
1. scope is per-thread
2. "Global" in the context of thread instance
3. each Thread just sees its own thread variable
ThreadLocal<Integer> ID = new ThreadLocal<>();
ID.set(99);
ID.get();
Processor :
Core 0 Core 1
----- -----
L1,L2 (Caches) L1,L3
L3 cache (common to both cores)
---- (system bus)
System Memory
Volatile keyword
marks variable as do not cache i.e. every read-write is directly from memory
single volatile variable, applies volatile behavior to all other variables visible in a thread (to maintain consistency)
*/
class Counter implements Runnable {
private volatile int value = 0;
public int getValue() {
return value;
}
public void incrementValue() {
this.value++;
}
public void decrementValue() {
this.value--;
}
private void fun() {
this.incrementValue();
System.out.println(Thread.currentThread().getName() + " Increments : " + this.getValue());
this.decrementValue();
System.out.println(Thread.currentThread().getName() + " Decrements : " + this.getValue());
}
private Lock lock = new ReentrantLock();
@Override
public void run() {
// fun();
/*
T1 Increments : 2
T4 Increments : 4
T3 Increments : 3
T2 Increments : 3
T2 Decrements : 0
T3 Decrements : 1
T4 Decrements : 2
T1 Decrements : 3
*/
/* M-1 */
// synchronized (this) {
// fun();
// }
/* M-1 & M-2 :
T1 Increments : 1
T1 Decrements : 0
T2 Increments : 1
T2 Decrements : 0
T4 Increments : 1
T4 Decrements : 0
T3 Increments : 1
T3 Decrements : 0
*/
/* M-2 Acquires the lock */
// lock.lock();
// try {
// fun();
// } finally {
// lock.unlock();
// }
/* M-3 tryLock - Acquires the lock only if it is free at the time of invocation */
// if (lock.tryLock()) {
// try {
// fun();
// } finally {
// lock.unlock();
// }
// }else{
// System.out.println(Thread.currentThread().getName()+" - Unable To lock");
// }
/*
T1 Increments : 1
T2 - Unable To lock
T4 - Unable To lock
T3 - Unable To lock
T1 Decrements : 0
*/
}
}
public class LockInterface {
public static void main(String[] args) {
Counter counter = new Counter();
new Thread(counter, "T1").start();
new Thread(counter, "T2").start();
new Thread(counter, "T3").start();
new Thread(counter, "T4").start();
}
}