-
Notifications
You must be signed in to change notification settings - Fork 473
Expand file tree
/
Copy pathAsyncResult.java
More file actions
49 lines (39 loc) · 1.13 KB
/
AsyncResult.java
File metadata and controls
49 lines (39 loc) · 1.13 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
package org.influxdb;
import java.util.function.Consumer;
public class AsyncResult<T> {
private final Object syncObject = new Object();
private boolean gotResult = false;
private T result = null;
private Throwable throwable = null;
T result() throws Throwable {
while (!this.gotResult) {
synchronized (this.syncObject) {
this.syncObject.wait();
}
}
if (this.throwable != null) {
throw this.throwable;
}
return this.result;
}
public final Consumer<T> resultConsumer = new Consumer<T>() {
@Override
public void accept(T t) {
synchronized (syncObject) {
result = t;
gotResult = true;
syncObject.notifyAll();
}
}
};
public final Consumer<Throwable> errorConsumer = new Consumer<Throwable>() {
@Override
public void accept(Throwable t) {
synchronized (syncObject) {
throwable = t;
gotResult = true;
syncObject.notifyAll();
}
}
};
}