Java CompletableFuture.acceptor

Java CompletableFuture.acceptor,java,multithreading,Java,Multithreading,我正在试验JDK8中的CompletableFutureAPI,并从中尝试acceptor()方法。请看下面的代码(然后我会提出我的担忧): 我们可以看到,acceptor()返回的CompletableFuture无法执行提供的操作 但是,如果取消对无限while循环的注释,则会得到以下输出: task did not complete task did not complete task did not complete val: 10.0 task did not complete tas

我正在试验JDK8中的
CompletableFuture
API,并从中尝试
acceptor()
方法。请看下面的代码(然后我会提出我的担忧):

我们可以看到,
acceptor()
返回的
CompletableFuture
无法执行提供的
操作

但是,如果取消对无限while循环的注释,则会得到以下输出:

task did not complete
task did not complete
task did not complete
val: 10.0
task did not complete
task did not complete
task did not complete
task did not complete
task finally completed
exiting main method
以下是Acceptor方法的文档:

public CompletableFuture<Void> acceptEither(CompletionStage<? extends T> other,
                                            Consumer<? super T> action)

Description copied from interface: CompletionStage
Returns a new CompletionStage that, when either this or the other given stage complete normally, is executed with the corresponding result as argument to the supplied action. See the CompletionStage documentation for rules covering exceptional completion.

public CompletableFuture acceptor(CompletionStage主线程不会在Fork-Join公共池中等待任务。公共池的

但是,此池和任何正在进行的处理将在程序System.exit(int)时自动终止。任何依赖异步任务处理在程序终止之前完成的程序都应在退出之前调用commonPool()。等待静止

您还可以使用CompletableFuture的其他方法使用自己的executor,这些方法接受executor的签名。这样,所有提交的任务都将在executor上执行

ExecutorService executor = Executors.newCachedThreadPool();
CompletableFuture.supplyAsync( () -> {...}, executor);
...
然后在主线程的末尾关闭执行器

executor.shutdown();

我尝试了以下操作:
Executors.newCachedThreadPool();
甚至提供了如下自定义执行器实现:Executor exec=newexecutor(){@Override public void execute(Runnable Runnable){new Thread(Runnable,“我的超人”).start();};但这些没有按预期工作。我仍然有同样的问题。有什么建议吗。@nawazish stackoverflow这三行对我来说工作正常。它在真正退出之前打印“exiting main method+val:10.0”。请确保将这两个调用都更改为
SupplySync()
CompletableFuture
将其传递给执行者。否则,您仍然会遇到与以前相同的问题。是的,它有效;非常感谢您教我一些新东西!再次感谢。
ExecutorService executor = Executors.newCachedThreadPool();
CompletableFuture.supplyAsync( () -> {...}, executor);
...
executor.shutdown();