“addListener()”和”addCallback()”到ListenableFuture的区别。

huangapple go评论63阅读模式
英文:

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,您提供 FutureCallbackRunnable 不会提供结果,因此如果需要结果,您需要额外的努力。

简而言之,如果您想要使用 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.

huangapple
  • 本文由 发表于 2020年9月17日 22:44:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63940557.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定