-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
84 lines (77 loc) · 2.17 KB
/
Main.java
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
import java.util.concurrent.locks.*;
class Main {
static Lock lock;
static double[] sharedData;
static int SD = 100, CS = 100, TH = 10;
// SD: shared data srray size
// CS: critical section executed per thread
// TH: number of threads
// Critical section updated shared data
// with a random value. If all values
// dont match then it was not executed
// atomically!
static void criticalSection() {
try {
double v = Math.random();
for(int i=0; i<SD; i++) {
if(i % (SD/4)==0) Thread.sleep(1);
sharedData[i] = v + v*sharedData[i];
}
}
catch(InterruptedException e) {}
}
// Checks to see if all values match. If not,
// then critical section was not executed
// atomically.
static boolean criticalSectionWasAtomic() {
double v = sharedData[0];
for(int i=0; i<SD; i++)
if(sharedData[i]!=v) return false;
return true;
}
// Unsafe thread executes CS N times, without
// holding a lock. This can cause CS to be
// executed non-atomically which can be detected.
//
// Safe thread executes CS N times, while
// holding a lock. This allows CS to always be
// executed atomically which can be verified.
static Thread thread(int n, boolean safe) {
Thread t = new Thread(() -> {
for(int i=0; i<CS; i++) {
if(safe) lock.lock();
criticalSection();
if(safe) lock.unlock();
}
log(n+": done");
});
t.start();
return t;
}
// Tests to see if threads execute critical
// section atomically.
static void testThreads(boolean safe) {
String type = safe? "safe" : "unsafe";
log("Starting "+TH+" "+type+" threads ...");
Thread[] threads = new Thread[TH];
for(int i=0; i<TH; i++)
threads[i] = thread(i, safe);
try {
for(int i=0; i<TH; i++)
threads[i].join();
}
catch(InterruptedException e) {}
boolean atomic = criticalSectionWasAtomic();
log("Critical Section was atomic? "+atomic);
log("");
}
public static void main(String[] args) {
lock = new TASLock();
sharedData = new double[SD];
testThreads(false);
testThreads(true);
}
static void log(String x) {
System.out.println(x);
}
}