Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
public class OrTimeoutDemo {
public static void main(String[] args) {
CompletableFuture orTimeout = CompletableFuture.supplyAsync(() -> {
try {
return getUsers();
} catch (Exception ex) {
return null;
.orTimeout(1, TimeUnit.SECONDS);
// Should be avoided, only for demonstration purpose
private static List getUsers() throws Exception {
// If this value will be more than 1000 i.e more than 1 second, timeout will be raised
List users = Arrays.asList("Deepak", "Ayush", "Nitesh", "Santosh", "Simran");
return users;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment