-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatientThread.java
More file actions
66 lines (55 loc) · 1.94 KB
/
PatientThread.java
File metadata and controls
66 lines (55 loc) · 1.94 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package BloodMatch.src.bloodmatch;
// a class, which manipulates with athread.
public class PatientThread implements Runnable {
Thread thrd;
String donor, patient;
boolean suspended;
// a constructor, which creates a Thread.
PatientThread(String id, String dId) {
donor = dId;
patient = id;
thrd = new Thread(this, id);
suspended = false;
}
// method, which creates and starts a request thread.
public static void createThread(String id, String dId, int priority) {
PatientThread myThrd = new PatientThread(id, dId);
myThrd.thrd.setPriority(priority);
Scheduler.queue.add(myThrd);
}
// method, which manipulates with requests.
public void run() {
// implement the suspantion.
synchronized (this) {
while (suspended) {
try {
this.wait();
} catch (InterruptedException exc) {
System.out.println("It seems, exception has been occurred: " + exc);
}
}
}
System.out.println("\nMaking operation for " + thrd.getName() + "...");
waitFor(1000);
System.out.println("Operation for " + thrd.getName() + " is completed.");
Scheduler.queue.remove(this); // remove this thread from queue.
Scheduler.triggerNext(); // wake up next thread.
}
// method, which waits for a particular time.
private synchronized void waitFor(int s) {
try {
Thread.sleep(s);
} catch (InterruptedException exc) {
System.out.println(exc);
}
}
// method, which suspends the thread.
public synchronized void requestSuspend() {
suspended = true;
}
// method, which resumes the thread.
public synchronized void requestResume() {
suspended = false;
notify();
}
}