Skip to content

Instantly share code, notes, and snippets.

@deepakmehra10
Created April 27, 2020 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deepakmehra10/0b312fb123abf8e25d2aac70362057d2 to your computer and use it in GitHub Desktop.
Save deepakmehra10/0b312fb123abf8e25d2aac70362057d2 to your computer and use it in GitHub Desktop.
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