英文:
Difference between addListener() and addCallback() to ListenableFuture
问题
我是Java并发库的初学者,正在学习在我的代码中使用ListenableFuture。我阅读了这个文档,但仍然对以下两种向ListenableFuture对象注册可运行代码的首选方式感到困惑:
future.addListener(Runnable, Executor)
vs Futures.addCallback(ListenableFuture<V>, FutureCallback<V>, Executor)
如果有人能够就性能、用例以及哪种方法优于其他方法提供一些指导,那将非常有帮助!
英文:
I am a beginner in Java concurrent library and learning to use ListenableFuture in my code. I went through this document and still confused about which one is the preferred way of registering runnable code to my ListenableFuture object:
future.addListener(Runnable, Executor)
vs Futures.addCallback(ListenableFuture<V>, FutureCallback<V>, Executor)
It will be really helpful if someone can throw light on performance, use-cases, and which one to prefer over others!
答案1
得分: 3
不同之处在于对于 addListener
,您提供 Runnable
,而对于 addCallback
,您提供 FutureCallback
。Runnable
不会提供结果,因此如果需要结果,您需要额外的努力。
简而言之,如果您想要使用 future 的结果,使用 addCallback
,否则使用 addListener
。
英文:
the difference is that for addListener
you supply Runnable
, and for addCallback
you sipply FutureCallback
. Runnable
is not provided with the result, so if you need it, you have to make additional efforts.
In short, if you want to use future's result, use addCallback
, otherwise use addListener
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论