Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between addListener() and addCallback() to ListenableFuture

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!

like image 743
abhilash_goyal Avatar asked Oct 23 '25 18:10

abhilash_goyal


1 Answers

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.

like image 174
Alexei Kaigorodov Avatar answered Oct 26 '25 06:10

Alexei Kaigorodov