• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RunnableFuture when would it be a good case to use it?

 
Nick Widelec
Ranch Hand
Posts: 226
Eclipse IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have stumbled upon this interface:

public interface RunnableFuture<V>
extends Runnable, Future<V>

A Future that is Runnable. Successful execution of the run method causes completion of the Future and allows access to its results.


However I am wondering what would be a good use of it?
Do you know any real life code example?

Thanks in advance.
 
Jayesh A Lalwani
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Usually you have Callable produce Futures. Basically, if you need to have a background process that returns result, you wrap that process in a class that implements Callable. When you submit the callable for execution, Java returns you a Future that wraps the result of the Callable. You can use the Future to keep track of the Callable, and also get the result of the Callable. So, if you want to create a background process, you need to implement
a) a class that implements Callable
b) a java bean that represents the result of the Callable

It's a little verbose, because it forces you to delineate between between business logic and data. If for some reason you don't want to deliniate and want a quick implementation where the the class that does the background processing also contains the result, you can implement RunnableFuture. It's basically a Runnable (something that does background processing ) and Future(the results of the background processing)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic