Created
April 27, 2020 15:00
-
-
Save deepakmehra10/0b312fb123abf8e25d2aac70362057d2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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