-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvent.java
More file actions
68 lines (56 loc) · 1.76 KB
/
Event.java
File metadata and controls
68 lines (56 loc) · 1.76 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
67
68
package eventscheduler;
// a class, which creates event threads.
public class Event implements Runnable {
EventProcessor task;
Thread thrd;
static int n = 0;
static Event[] eventThreads = new Event[10];
boolean suspended;
boolean stopped;
// a constructor for Event thread.
Event(String n, int p) {
thrd = new Thread(this, n);
suspended = false;
stopped = false;
}
// a factorial method, which creates and starts the thead.
public static Event createAndStart(String name, int p) {
Event e = new Event(name, p);
e.thrd.setPriority(p);
eventThreads[n] = e;
e.thrd.start(); // start the thread.
n++;
return e;
}
// enter a thread.
public void run() {
System.out.println("[New Event] " + thrd.getName() + " (priority: " +
thrd.getPriority() + ")");
synchronized (this) {
while (suspended) {
try {
wait();
} catch (InterruptedException exc) {
System.out.println("Exception while waiting on thread " + thrd.getName());
}
}
}
Scheduler.schedule(eventThreads);
EventProcessor.process(thrd.getName());
}
// a method, which suspends the thread.
public synchronized void requestSuspend() {
suspended = true;
}
// a method, which resumes the thread.
public synchronized void requestResume() {
suspended = false;
notify();
}
// a method, which stops the thread.
public synchronized void requestStop() {
stopped = true;
suspended = false;
notify();
}
}