Skip to content

Instantly share code, notes, and snippets.

Embed
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) {
System.out.println(ex.getMessage());
return null;
}
})
.orTimeout(1, TimeUnit.SECONDS);
// Should be avoided, only for demonstration purpose
System.out.println(orTimeout.join());
}
private static List getUsers() throws Exception {
// If this value will be more than 1000 i.e more than 1 second, timeout will be raised
Thread.sleep(100);
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