Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java8,可完成的未来_Java_Java 8 - Fatal编程技术网

Java8,可完成的未来

Java8,可完成的未来,java,java-8,Java,Java 8,在下面的代码片段中,有人能告诉我调用CompletableFuture.SupplySync时会发生什么情况吗?当返回值时,它会在单独的线程中返回吗 // Run a task specified by a Supplier object asynchronously CompletableFuture<String> future = CompletableFuture.supplyAsync(new Supplier<String>() { @Override

在下面的代码片段中,有人能告诉我调用CompletableFuture.SupplySync时会发生什么情况吗?当返回值时,它会在单独的线程中返回吗

// 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);
这将返回一个新的完全未来对象,该对象基本上 在从ForkJoinPool借用的新线程中执行。请参考javadoc-

/**
 * Returns a new CompletableFuture that is asynchronously completed
 * by a task running in the {@link ForkJoinPool#commonPool()} with
 * the value obtained by calling the given Supplier.
 *
 * @param supplier a function returning the value to be used
 * to complete the returned CompletableFuture
 * @param <U> the function's return type
 * @return the new CompletableFuture
 */
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier) {
    return asyncSupplyStage(asyncPool, supplier);
}

提供的操作是非阻塞的,结果将在可用时返回