-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomLock.java
More file actions
50 lines (44 loc) · 1.54 KB
/
Copy pathCustomLock.java
File metadata and controls
50 lines (44 loc) · 1.54 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
//custom lock class to support mutual exclusion
public class CustomLock {
public static int numThreads = 4;
public static volatile int count = 0;
public static TaskPool taskpool;
/**
*turn represents a true or false value if it is a threads turn or not
*priority is the order which each thread runs
*priority is found by adding one to the priority of the thread before it
*/
private static volatile boolean[] turn = new boolean[numThreads];
private static volatile int[] priority = new int[numThreads];
//lock method to secure a thread runs, start of mutual exclusion
public void lock(int label) {
//turn is set to true and priority is found
turn[label] = true;
priority[label] = findCount() + 1;
//turn is now set to false after priority has been set
turn[label] = false;
for(int i = 0; i< numThreads;i++) {
//if thread is current thread move on to next
if(i == label) {
continue;
}
//do nothing if the priority is not 0 and less than the thread label passed into method
while(priority[i] != 0 && (priority[label] > priority[i] || (priority[label] == priority[i] && label > i))) {
}
}
}
//method releasing lock and setting priority to 0 so thread does not run again
public void unlock(int label) {
priority[label] = 0;
}
//method for finding getting priority of each thread returns max value
public int findCount() {
int x = priority[0];
for(int i = 1; i< priority.length; i++) {
if(priority[i] > x) {
x = priority[i];
}
}
return x;
}
}