-
Notifications
You must be signed in to change notification settings - Fork 0
/
GmoCompletableFutureDemoJava9.java
47 lines (34 loc) · 1.47 KB
/
GmoCompletableFutureDemoJava9.java
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
package challenge2.java9;
import externalLegacyCodeNotUnderOurControl.PriceService;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
public class GmoCompletableFutureDemoJava9 {
/***** JAVA9 Code disabled, in order not to break build.
private PriceService service = new PriceService(5);
private void run() {
ExecutorService executor = Executors.newFixedThreadPool(1);
Supplier<Integer> sup_getprice = () -> service.getPrice();
CompletableFuture.supplyAsync(sup_getprice, executor)
.completeOnTimeout(42, 2, TimeUnit.SECONDS)
.thenAccept(amount -> {
System.out.println("Price with timeout is: " + amount);
// TODO: do not shutdown whole executor, but only the CompletableFuture
// => Is there a better solution?
executor.shutdownNow(); //send interrupt to PriceService, if it is still running
});
try {
System.out.println("Main thread waiting for completion.");
Thread.sleep(10 * 1000);
System.out.println("Main thread done.");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new GmoCompletableFutureDemoJava9().run();
}
*/
}