Skip to content
This repository was archived by the owner on Jun 20, 2022. It is now read-only.

Commit 088fc19

Browse files
committed
Add critical section notes
1 parent f8aa3ad commit 088fc19

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public abstract class MutualExclusionAlgorithm {
2+
private static final int MAX_WAIT_TIME = 3000;
3+
4+
public abstract void requestCriticalSection(int t);
5+
6+
public abstract void notifyDone(int t);
7+
8+
public static void doCriticalWork() {
9+
try {
10+
// Simulate work
11+
Thread.sleep((int) (Math.random() * MAX_WAIT_TIME));
12+
} catch (InterruptedException e) {
13+
// Do nothing
14+
}
15+
}
16+
17+
public static void doWork() {
18+
try {
19+
// Simulate work
20+
Thread.sleep((int) (Math.random() * MAX_WAIT_TIME));
21+
} catch (InterruptedException e) {
22+
// Do nothing
23+
}
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class PetersonsAlgorithm extends MutualExclusionAlgorithm {
2+
private volatile int turn = 0;
3+
private volatile boolean[] threadWantsCriticalSection = {false, false};
4+
5+
public void requestCriticalSection(int threadId) {
6+
int otherThreadId = 1 - threadId;
7+
8+
threadWantsCriticalSection[threadId] = true;
9+
turn = otherThreadId;
10+
while (threadWantsCriticalSection[otherThreadId] && turn == otherThreadId) {
11+
Thread.yield();
12+
}
13+
}
14+
15+
public void notifyDone(int threadId) {
16+
threadWantsCriticalSection[threadId] = false;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
public class PetersonsAlgorithmTest {
2+
public static void main(String[] args) {
3+
MutualExclusionAlgorithm mutexAlg = new PetersonsAlgorithm();
4+
5+
Worker thread1 = new Worker("Thread 1", 0, mutexAlg);
6+
Worker thread2 = new Worker("Thread 2", 1, mutexAlg);
7+
8+
thread1.start();
9+
thread2.start();
10+
}
11+
}
12+
13+
class Worker extends Thread {
14+
private String name;
15+
private int threadId;
16+
private MutualExclusionAlgorithm mutexAlg;
17+
18+
public Worker(String name, int threadId, MutualExclusionAlgorithm mutexAlg) {
19+
this.name = name;
20+
this.threadId = threadId;
21+
this.mutexAlg = mutexAlg;
22+
}
23+
24+
public void run() {
25+
while (true) {
26+
System.out.printf("%s: requesting critical section\n", name);
27+
mutexAlg.requestCriticalSection(threadId);
28+
System.out.printf("%s: entered critical section\n", name);
29+
long startTime = System.nanoTime();
30+
31+
System.out.printf("%s: doing critical work\n", name);
32+
MutualExclusionAlgorithm.doCriticalWork();
33+
34+
mutexAlg.notifyDone(threadId);
35+
long endTime = System.nanoTime();
36+
double duration = (endTime - startTime) / 1000000.0;
37+
System.out.printf("%s: left critical section (critical work lasted for %f ms)\n", name, duration);
38+
39+
System.out.printf("%s: doing normal work\n", name);
40+
MutualExclusionAlgorithm.doWork();
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)