-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActor.java
More file actions
65 lines (54 loc) · 1.72 KB
/
Actor.java
File metadata and controls
65 lines (54 loc) · 1.72 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
package example7;
import example6.MutableQueue;
import java.util.concurrent.*;
public abstract class Actor {
abstract protected Object receive(Object message);
public Future<Object> send(Object message) {
CompletableFuture<Object> future = new CompletableFuture<>();
MessageAndFuture pair = new MessageAndFuture(message, future);
messages.enqueue(pair);
return future;
}
private final Thread thread = createThread();
private volatile boolean isRunning = true;
private final MutableQueue<MessageAndFuture> messages =
new MutableQueue<>();
private void loop() {
while (isRunning) {
MessageAndFuture pair = null;
boolean gotMessage;
try {
pair = messages.awaitDequeue(TimeUnit.SECONDS, 1);
gotMessage = true;
} catch (TimeoutException e) {
gotMessage = false;
}
if (gotMessage)
try {
Object result = receive(pair.message);
pair.future.trySuccess(result);
}
catch (Throwable th) {
pair.future.tryFailure(th);
}
}
}
private Thread createThread() {
Thread th = new Thread(new Runnable() {
@Override
public void run() {
loop();
}
});
th.start();
return th;
}
public void join(TimeUnit unit, int timeout)
throws TimeoutException, InterruptedException {
messages.awaitEmpty(unit, timeout);
}
public void shutdown() throws InterruptedException {
isRunning = false;
thread.join(10000);
}
}