forked from hansiming/JavaProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCriticalSection.java
More file actions
206 lines (141 loc) · 3.35 KB
/
CriticalSection.java
File metadata and controls
206 lines (141 loc) · 3.35 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package com.csdhsm.concurrent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
//not thread safe
class Pair{
private int x,y;
public Pair(int x, int y){
this.x = x;
this.y = y;
}
public Pair() {
this(0,0);
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public void incrementX(){
x++;
}
public void incrementY(){
y++;
}
@Override
public String toString() {
return "x:" + x + " y:" + y;
}
@SuppressWarnings("serial")
public class PairValueNotEquealException extends RuntimeException{
public PairValueNotEquealException() {
super("Pari values not equal: " + Pair.this);
}
}
public void checkState(){
if(x != y){
throw new PairValueNotEquealException();
}
}
}
abstract class PairManager{
AtomicInteger checkCounter = new AtomicInteger(0);
protected Pair pair = new Pair();
private List<Pair> storage = Collections.synchronizedList(new ArrayList<Pair>());
public synchronized Pair getPair(){
//Make a copy to keep the original safe
return new Pair(pair.getX(), pair.getY());
}
protected void store(Pair p){
storage.add(p);
try {
TimeUnit.SECONDS.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public abstract void increment();
}
//Synchronized the entire method
class PairManager1 extends PairManager{
@Override
public synchronized void increment() {
pair.incrementX();
pair.incrementY();
store(pair);
}
}
//Use a critical section
class PairManager2 extends PairManager{
@Override
public void increment() {
Pair temp;
synchronized(this){
pair.incrementX();
pair.incrementY();
temp = getPair();
}
store(temp);
}
}
class PairManipulator implements Runnable{
private PairManager pm;
public PairManipulator(PairManager pm) {
this.pm = pm;
}
@Override
public void run() {
while(true){
pm.increment();
}
}
@Override
public String toString() {
return "Pair: " + pm.getPair() + " checkCounter =" + pm.checkCounter.get();
}
}
class PairChecker implements Runnable{
PairManager pm;
public PairChecker(PairManager pm) {
this.pm = pm;
}
@Override
public void run() {
while(true){
pm.checkCounter.incrementAndGet();
pm.getPair().checkState();
}
}
}
public class CriticalSection {
//Test the two different approaches
static void testApproaches(PairManager pman1, PairManager pman2){
ExecutorService service = Executors.newCachedThreadPool();
PairManipulator pm1 = new PairManipulator(pman1);
PairManipulator pm2 = new PairManipulator(pman2);
PairChecker pcheck1 = new PairChecker(pman1);
PairChecker pcheck2 = new PairChecker(pman2);
service.execute(pm1);
service.execute(pm2);
service.execute(pcheck1);
service.execute(pcheck2);
try{
TimeUnit.MILLISECONDS.sleep(500);
}catch (Exception e) {
System.out.println("Sleep Interrupted");
}
System.out.println("pm1: " + pm1 + "\npm2: " + pm2);
System.exit(0);
}
public static void main(String[] args) {
PairManager pman1 = new PairManager1();
PairManager pman2 = new PairManager2();
testApproaches(pman1, pman2);
}
}