Closed dunmengjun closed 6 years ago
I suggest adding an async static method to the Async class,like this
public static <T>CompletableFuture<T> async(Supplier<T> supplier){ return CompletableFuture.supplyAsync(supplier); } public static <T> CompletableFuture<T> async(T object){ return CompletableFuture.completedFuture(object); }
The reason is that providing a more convenient way to create asynchronous tasks is the default choice
The program will be written like this
public class TestMain { public static void main(String[] args) throws InterruptedException { Async.init(); TestMain main = new TestMain(); CompletableFuture<Boolean> call = main.call(); // Boolean aBoolean = call.get(); // System.out.println(aBoolean); call.thenAccept((result) -> { System.out.println(result); }); Thread.sleep(10000); } public CompletableFuture<Boolean> call(){ Boolean await = await(isExist()); Boolean await1 = await(timeout()); return async(await); } public CompletableFuture<Boolean> isExist(){ return async(() -> true); } public CompletableFuture<Boolean> timeout(){ return async(() -> { try { TimeUnit.MILLISECONDS.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } return false; }); } }
This will hide the details of constructing CompletableFuture What do you think?
I suggest adding an async static method to the Async class,like this
The reason is that providing a more convenient way to create asynchronous tasks is the default choice
The program will be written like this
This will hide the details of constructing CompletableFuture What do you think?