-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMCSLockThread.java
More file actions
39 lines (38 loc) · 1.02 KB
/
MCSLockThread.java
File metadata and controls
39 lines (38 loc) · 1.02 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
import java.util.Random;
public class MCSLockThread extends Thread {
static MCSLock l = new MCSLock();
Random rand = new Random();
MCSLockQNode qnode = new MCSLockQNode();
boolean doLock = true;
public MCSLockThread(int id) {
super(id);
}
public Method nextMethod() {
Method m = null;
if(rand.nextInt(10) > 5) {
m = new Sleep(rand.nextInt(3), getThreadId());
} else {
if(doLock) {
m = new MCSLockLock(l, qnode, getThreadId());
} else {
m = new MCSLockUnlock(l, qnode, getThreadId());
}
doLock = !doLock;
}
return m;
}
public boolean betweenMethods() {
return currentMethod == null;
}
public String getState() {
return qnode.toString();
}
public static void main(String args[]) {
MCSLockThread threads[] = new MCSLockThread[2];
for (int i = 0; i < threads.length; i++) {
threads[i] = new MCSLockThread(i);
}
iterate(threads);
//System.out.println(s);
}
}