草庐IT

CompletableFuture

全部标签

java - ExecutorService.submit(Task) vs CompletableFuture.supplyAsync(Task, Executor)

要并行或异步运行一些东西,我可以使用ExecutorService:Futuresubmit(Runnabletask,Tresult);或CompletableFutureAPI:staticCompletableFuturesupplyAsync(Suppliersupplier,Executorexecutor);(假设我在这两种情况下都使用同一个Executor)除了返回类型Future与CompletableFuture有什么显着差异。或者什么时候用什么?如果我使用CompletableFuture有什么区别?默认APIExecutor(没有执行者的方法)?

java - Java CompletableFuture 的 thenApply 和 thenApplyAsync 有什么区别?

假设我有以下代码:CompletableFuturefuture=CompletableFuture.supplyAsync(()->0);thenApply案例:future.thenApply(x->x+1).thenApply(x->x+1).thenAccept(x->System.out.println(x));这里的输出将是2。现在在thenApplyAsync的情况下:future.thenApplyAsync(x->x+1)//firststep.thenApplyAsync(x->x+1)//secondstep.thenAccept(x->System.out.pr

java - Java CompletableFuture 的 thenApply 和 thenApplyAsync 有什么区别?

假设我有以下代码:CompletableFuturefuture=CompletableFuture.supplyAsync(()->0);thenApply案例:future.thenApply(x->x+1).thenApply(x->x+1).thenAccept(x->System.out.println(x));这里的输出将是2。现在在thenApplyAsync的情况下:future.thenApplyAsync(x->x+1)//firststep.thenApplyAsync(x->x+1)//secondstep.thenAccept(x->System.out.pr

java - 返回 CompletableFuture<Void> 还是 CompletableFuture<?>?

我想编写一个返回CompletableFuture的异步方法.future的唯一目的是跟踪方法何时完成,而不是其结果。返回CompletableFuture会更好吗?或CompletableFuture?是否有理由偏爱其中一个,或者它们可以互换?CompletableFuture本身返回CompletableFuture来自它的许多方法。java.nio有一个Future在AsynchronousSocketChannel:Futureconnect(SocketAddressremote).另一方面,java.util.concurrent类如ExecutorService和Sche

java - 返回 CompletableFuture<Void> 还是 CompletableFuture<?>?

我想编写一个返回CompletableFuture的异步方法.future的唯一目的是跟踪方法何时完成,而不是其结果。返回CompletableFuture会更好吗?或CompletableFuture?是否有理由偏爱其中一个,或者它们可以互换?CompletableFuture本身返回CompletableFuture来自它的许多方法。java.nio有一个Future在AsynchronousSocketChannel:Futureconnect(SocketAddressremote).另一方面,java.util.concurrent类如ExecutorService和Sche

java - CompletableFuture/ForkJoinPool 集合类加载器

我解决了一个非常具体的问题,其解决方案似乎是基本的:我的(Spring)应用程序的类加载器层次结构是这样的:SystemClassLoader->PlatformClassLoader->AppClassLoader如果我使用JavaCompleteableFuture来运行线程。线程的ContextClassLoader为:SystemClassLoader->PlatformClassLoader->ThreadClassLoader因此,我无法访问AppClassLoader中的任何类,尽管我必须这样做,因为所有外部库类都驻留在那里。源代码库非常大,所以我不想/不能将所有与线程相

java - CompletableFuture/ForkJoinPool 集合类加载器

我解决了一个非常具体的问题,其解决方案似乎是基本的:我的(Spring)应用程序的类加载器层次结构是这样的:SystemClassLoader->PlatformClassLoader->AppClassLoader如果我使用JavaCompleteableFuture来运行线程。线程的ContextClassLoader为:SystemClassLoader->PlatformClassLoader->ThreadClassLoader因此,我无法访问AppClassLoader中的任何类,尽管我必须这样做,因为所有外部库类都驻留在那里。源代码库非常大,所以我不想/不能将所有与线程相

java - 如何使用 Spring 制作异步 REST?

我正在尝试使用SpringBoot制作一个小型REST。没用过Spring,很久以前用过Java(Java7)!在过去的2年里,我只使用过Python和C#(但就像我说的,我已经使用过Java)。所以,现在,我正在尝试使用异步方法制作REST,并检查了几个示例,但我仍然不太了解执行此操作的“正确方法”。查看以下文档:http://carlmartensen.com/completablefuture-deferredresult-async,Java8有CompletableFuture可以与Spring一起使用,所以,我编写了以下代码:服务:@ServicepublicclassUs

java - 如何使用 Spring 制作异步 REST?

我正在尝试使用SpringBoot制作一个小型REST。没用过Spring,很久以前用过Java(Java7)!在过去的2年里,我只使用过Python和C#(但就像我说的,我已经使用过Java)。所以,现在,我正在尝试使用异步方法制作REST,并检查了几个示例,但我仍然不太了解执行此操作的“正确方法”。查看以下文档:http://carlmartensen.com/completablefuture-deferredresult-async,Java8有CompletableFuture可以与Spring一起使用,所以,我编写了以下代码:服务:@ServicepublicclassUs

Spring @Async 与 CompletableFuture

我对这段代码有疑问:@AsyncpublicCompletableFuturedoFoo(){CompletableFuturefooFuture=newCompletableFuture();try{StringfooResult=longOp();fooFuture.complete(fooResult);}catch(Exceptione){fooFuture.completeExceptionally(e);}returnfooFuture;}问题是:doFoo是否仅在longOp完成(正确或异常)之后才返回fooFuture并因此返回已经完成的future,还是Spring做