orTimeout() method of CompletableFuture object in Java

This method was introduced from Java 9.

This method is used to specify that if our task does not complete within a certain period of time, the program will stop and be TimeoutException.

In the following example, I want to calculate the sum of two numbers that will be completed in 1 second, but in the process of summing, I set up it sleep 3s.

Result:

orTimeout() method of CompletableFuture object in Java

If I now increase the timeout time:

the program will not fail anymore.

orTimeout() method of CompletableFuture object in Java

 

Add Comment