# Future, CompletableFuture

<figure><img src="https://415484505-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LxtoAXZwwOc4XGto8vb%2Fuploads%2FJXlb7nf63cqfGQWovAPE%2FScreenshot%202023-11-28%20at%2009.35.41.png?alt=media&#x26;token=d1698428-e94c-47dc-86b6-b56f67564644" alt=""><figcaption></figcaption></figure>

Below described a problem, when several Futures are submitted and you try to wait for them using `.get()` in a loop. There is no guarantee which will be the order of all these Futures execution. So your main thread will wait until your first Future in a loop will return anything:

<figure><img src="https://415484505-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LxtoAXZwwOc4XGto8vb%2Fuploads%2F3ppVCRjRxcYUfLoYsIUg%2FScreenshot%202023-11-28%20at%2009.40.44.png?alt=media&#x26;token=866a921f-8ec6-44bc-aa26-37a7185df8d1" alt=""><figcaption></figcaption></figure>

## Completable future

```java
Supplier<String> supplier = () -> "Hello";
CompletableFuture<String> future = CompletableFuture.supplyAsync(supplier);
future.join(); // this is a blocking code
```

### ofAll

{% code fullWidth="true" %}
```java
CompletableFuture<Weather> future1 = CompletableFuture.supplyAsync(new WeatherRetriever()::getWeather);
CompletableFuture<Weather> future2 = CompletableFuture.supplyAsync(new WeatherRetriever()::getWeather);
CompletableFuture<Weather> future3 = CompletableFuture.supplyAsync(new WeatherRetriever()::getWeather);

CompletableFuture<Void> done = CompletableFuture.allOf(future1, future2, future3);
done.thenApply( v -> Stream.of(future1, future2, future3)
// this is kind of blocking, but at this moment all futures are completed
    .map(CompletableFuture::join)
).join();

```
{% endcode %}

### Composing results of futures

{% code fullWidth="true" %}
```java
CompletableFuture<TheJump> jumpFuture = CompletableFuture.supplyAsync(Jumper::jump);
CompletableFuture<TheSwim> swimFuture = CompletableFuture.supplyAsync(Swimmer::swim);

SportResult result = jumpFuture.thenCompose(
		jumpResult -> swimFuture.thenApply(
				swimResult -> new SportResult(jumpResult, swimResult)
		)
).join();
```
{% endcode %}

<figure><img src="https://415484505-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LxtoAXZwwOc4XGto8vb%2Fuploads%2FoPKF4bm9ScWZDHd7o21m%2FScreenshot%202023-12-08%20at%2017.01.30.png?alt=media&#x26;token=47e1d3b4-eb61-4c1c-bef1-e5085c3e5abd" alt=""><figcaption></figcaption></figure>

<figure><img src="https://415484505-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LxtoAXZwwOc4XGto8vb%2Fuploads%2FFXSMDgz0gsQiROpNhThL%2FScreenshot%202023-12-08%20at%2017.02.49.png?alt=media&#x26;token=5efab6f4-9f16-415f-a06a-ef895fe7d1b9" alt=""><figcaption></figcaption></figure>

<figure><img src="https://415484505-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LxtoAXZwwOc4XGto8vb%2Fuploads%2F56p5NsNKjDtRgGgtPRLS%2FScreenshot%202023-12-08%20at%2017.09.37.png?alt=media&#x26;token=f5bda1d4-a806-4aec-a30c-0d72e7e9b8f4" alt=""><figcaption></figcaption></figure>

<figure><img src="https://415484505-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LxtoAXZwwOc4XGto8vb%2Fuploads%2FX0wAm5Vru6RYB5VCLGI9%2FScreenshot%202023-12-08%20at%2017.12.11.png?alt=media&#x26;token=fc4f1017-92eb-4295-9a4f-16fc69a04bba" alt=""><figcaption></figcaption></figure>

<figure><img src="https://415484505-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LxtoAXZwwOc4XGto8vb%2Fuploads%2FtonRDbwi14gUBddDU12D%2FScreenshot%202023-12-08%20at%2017.16.27.png?alt=media&#x26;token=6a5ea530-0160-4b33-abba-3abeda01f9b7" alt=""><figcaption></figcaption></figure>
