-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathStockPriceDataFetcher.java
52 lines (38 loc) · 1.64 KB
/
StockPriceDataFetcher.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
package com.javatechie.async;
import java.util.concurrent.CompletableFuture;
public class StockPriceDataFetcher {
public CompletableFuture<Double> fetchStockPriceFromApi1(String symbol) {
return CompletableFuture.supplyAsync(() -> {
// Simulate a network delay
simulateDelay(2000); // Simulate a delay of 2 seconds
return 150.0; // Simulated stock price from API 1
});
}
public CompletableFuture<Double> fetchStockPriceFromApi2(String symbol) {
return CompletableFuture.supplyAsync(() -> {
// Simulate a network delay
simulateDelay(1000); // Simulate a delay of 3 seconds
return 155.0; // Simulated stock price from API 2
});
}
private void simulateDelay(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
public static void main(String[] args) {
StockPriceDataFetcher fetcher = new StockPriceDataFetcher();
String stockSymbol = "APPL"; //apple inc
//fetch stock price from both the API
CompletableFuture<Double> api1Results = fetcher.fetchStockPriceFromApi1(stockSymbol);
CompletableFuture<Double> api2Results = fetcher.fetchStockPriceFromApi2(stockSymbol);
//Use anyOf to wait any of the future to complete
CompletableFuture<Object> anyOfResults = CompletableFuture.anyOf(api1Results, api2Results);
//process the result
anyOfResults.thenAccept(price -> {
System.out.println("Received stock price : $" + price);
}).join();
}
}