java8 异步方法 CompletableFuture.runAsync 不会 运行

java8 asynchronous method CompletableFuture.runAsync doesn't run

运行 异步方法的非常基本的代码。 当我 运行 以下代码时 运行Async 不会 运行。 我错过了什么?

结果运行只有同步码。

public class Main {

    public static void main(String[] args) {
        runAsync("run async command ans wait 10000");

        System.out.println("sync commands ");
    }

    public static void runAsync(String inputStr) {

       CompletableFuture.runAsync(() -> {
            List<String> strings = Arrays.asList(inputStr.split(" "));
            int sleep = Integer.parseInt(strings.get(strings.size() - 1));
            try {
                sleep(sleep);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println("async command ");
        });

    }
}

我希望先得到 "sync commands" 然后 "async command " 但只得到同步消息

您的任务将 运行 在其他 Thread 中完成(默认情况下在 ForkJoinPool 中的 Thread 中)并且您不会等待它完成 - 主要Thread 在您的异步任务 executed/submitted 之前结束。您可以调用 CompletableFuture::join 等待它完成,它会阻塞主 Thread 直到它完成:

CompletableFuture.runAsync(() -> {
        List<String> strings = Arrays.asList(inputStr.split(" "));
        int sleep = Integer.parseInt(strings.get(strings.size() - 1));
        try {
            sleep(sleep);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("async command ");
}).join(); //here call the join

或喜欢:

CompletableFuture<Void> cf = CompletableFuture.runAsync(() -> {
   //...
});

cf.join();

它确实 运行,但它 运行 在另一个线程上,您不会等待或对结果做任何事情。正如 CompletableFuture.runAsync() 的 Javadoc 所说:

Returns a new CompletableFuture that is asynchronously completed by a task running in the ForkJoinPool.commonPool() after it runs the given action.

runAsync() 对于没有 return 任何东西的任务很有用。如果你想要它的结果,你应该使用 supplyAsync() which returns a CompletableFuture<T> 然后你可以从中得到结果:

// Run a task specified by a Supplier object asynchronously
CompletableFuture<String> future = CompletableFuture.supplyAsync(new Supplier<String>() {
    @Override
    public String get() {
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new IllegalStateException(e);
        }
        return "Result of the asynchronous computation";
    }
});

// Block and get the result of the Future
String result = future.get();
System.out.println(result);

您需要等待使用join完成异步任务,例如:

public static void main(String[] args) {
    CompletableFuture<Void> future = runAsync("run async command ans wait 10000");
    future.join();
    System.out.println("sync commands ");
}

public static CompletableFuture<Void> runAsync(String inputStr) {
    return CompletableFuture.runAsync(() -> {
        List<String> strings = Arrays.asList(inputStr.split(" "));
        int sleep = Integer.parseInt(strings.get(strings.size() - 1));
        try {
            sleep(sleep);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("async command ");
    });
}