|
|
|
|
|
by cogman10
397 days ago
|
|
Also, arguably, the wrong way to do something like this. The author uses `setTimeout` for javascript. The equivalent for Java is either the `Timer` class or a `ScheduledExecutorService`. Doing a `Thread.sleep` simply isn't how you should approach this. With that in mind, if you want to use both these things and keep the completable future interface you'd have to do soemthing like this. ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
var future = new CompletableFuture<String>();
scheduler.schedule(()->future.complete("Data Fetched"), 10, TimeUnit.SECONDS);
future.thenAccept(result -> System.out.println(result));
System.out.println("Prints first"); // prints before the async result
scheduler.shutdown();
|
|