-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyThread.java
More file actions
29 lines (27 loc) · 914 Bytes
/
Copy pathMyThread.java
File metadata and controls
29 lines (27 loc) · 914 Bytes
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
//class for threads to call lock and unlock
public class MyThread extends Thread {
public static TaskPool taskpool;
private static CustomLock myLock = new CustomLock();
public int threadLabel;
//each thread to be created will have a specific label
public MyThread(int label) {
threadLabel = label;
}
@Override
public void run() {
System.out.println("thread " + threadLabel + " is attempting to get the lock");
myLock.lock(threadLabel);
System.out.println("thread" + threadLabel + " has succesfully got the lock");
//critical section
try {
System.out.println(threadLabel + " attempting to get task " + threadLabel);
Task task = TaskPool.getTask(threadLabel);
task.run();
sleep(1000);
}catch(InterruptedException e){
}
System.out.println("thread " + threadLabel + " is now releasing lock");
//releases lock for next thread, ends critical section
myLock.unlock(threadLabel);
}
}