-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathTestAOT.java
More file actions
149 lines (126 loc) · 4.29 KB
/
TestAOT.java
File metadata and controls
149 lines (126 loc) · 4.29 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* @author: Damir Ljubic
* @email: damirlj@yahoo.com
* <p>All rights reserved!
*/
package <your own package>;
import androidx.annotation.NonNull;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
public class TestAOT {
@NonNull
private <R> List<Future<R>> submitTasks_withResults(
@NotNull List<Callable<R>> callables, @NotNull AOThread aot) {
List<Future<R>> results = new ArrayList<>(callables.size());
for (Callable<R> callable : callables) {
aot.enqueueWithResult(callable).map(results::add);
}
return results;
}
private <R> void fetchResultOrException(
@NonNull Future<R> result, @NotNull Consumer<R> callback) {
try {
callback.accept(result.get()); // blocking call: wait on result being set
} catch (ExecutionException | InterruptedException e) { // or exception being thrown
e.printStackTrace();
}
}
private <R> void syncOnResults(@NotNull List<Future<R>> results, @NotNull Consumer<R> callback) {
for (Future<R> result : results) {
fetchResultOrException(result, callback);
}
}
@NotNull
private Callable<String> createCallable(long timeout) {
return () -> {
Thread.sleep(timeout); // simulate some work
return String.format( // it's always hosted by the same thread - execution context
"(thread: %s): Sleeping for %d[ms]", Thread.currentThread().getName(), timeout);
};
}
@Test
public void test_AOTWaitingOnResults() throws InterruptedException {
final AOThread aot = new AOThread();
aot.start();
Thread client =
new Thread(
() -> {
final List<Callable<String>> callables =
List.of(createCallable(200L), createCallable(100L), createCallable(300L));
final List<Future<String>> results = submitTasks_withResults(callables, aot);
// Client: synchronized on the result
syncOnResults(results, System.out::println);
},
"t_client");
client.start();
client.join();
aot.stopNow();
}
@NotNull
private AOThread.IJob createJob(long timeout) {
return () -> {
Thread.sleep(timeout); // simulate some work
System.out.printf(
"Executing within thread: \"%s\", after %d[ms]\n",
Thread.currentThread().getName(), timeout);
};
}
@Test
public void test_AOTWithoutWaitingOnResults() throws InterruptedException {
final AOThread aot = new AOThread();
aot.start();
Thread client =
new Thread(
() -> {
final List<AOThread.IJob> callables = List.of(createJob(200L), createJob(100L));
for (AOThread.IJob callable : callables) {
aot.enqueue(callable);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},
"t_client");
client.start();
client.join();
aot.stop(1, TimeUnit.SECONDS);
}
@Test
public void test_bothOfThem() throws InterruptedException {
final AOThread aot = new AOThread();
aot.start();
Thread client =
new Thread(
() -> {
// Submit, without waiting
aot.enqueue(createJob(100L));
aot.enqueue(() -> {}); // this will not brake the looper
// Wait on result, or exception being set
Optional<CompletableFuture<String>> opFut =
aot.enqueueWithResult(createCallable(200L));
opFut.ifPresent(
fut ->
fetchResultOrException(
fut,
res ->
System.out.printf(
"(Thread: \"%s\") Receiving result: %s\n",
Thread.currentThread().getName(), res)));
},
"t_client");
client.start();
client.join();
aot.stopNow();
}
}