-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathFibonacci.java
52 lines (49 loc) · 1.72 KB
/
Fibonacci.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
48
49
50
51
52
import java.util.concurrent.Executor;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.net.URI;
import java.net.http.HttpRequest;
import java.net.http.HttpClient;
import java.time.Duration;
import java.io.IOException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
/**
* fib of 47 takes ~11s
* fib of 49 fakes ~30s
*/
public class Fibonacci {
public static long fib(int n) {
if (n <= 2)
return 1;
return fib(n - 1) + fib(n - 2);
}
public static CompletableFuture<Long> parallelFib(int n, Executor executor) {
CompletableFuture<Long> partOne = CompletableFuture.supplyAsync(() -> fib(n - 1), executor);
CompletableFuture<Long> partTwo = CompletableFuture.supplyAsync(() -> fib(n - 2), executor);
return partOne.thenCombineAsync(partTwo, (resultOne, resultTwo) -> resultOne + resultTwo, executor);
}
public static CompletableFuture<Long> fibWithRequest(int i, int n, Executor executor) {
return CompletableFuture.supplyAsync(() -> fib(n), executor)
.thenComposeAsync(result -> {
var request = HttpRequest.newBuilder()
.GET()
.uri(URI.create("https://httpbin.org/delay/5"))
.build();
var httpClient = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.connectTimeout(Duration.ofSeconds(30))
.executor(executor)
.build();
try {
System.out.printf("Thread %d - requesting%n", i);
return httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApplyAsync(resp -> result);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}, executor);
}
}