aspose file tools*
The moose likes Threads and Synchronization and the fly likes RunnableFuture when would it be a good case to use it? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "RunnableFuture when would it be a good case to use it?" Watch "RunnableFuture when would it be a good case to use it?" New topic
Author

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

Nick Widelec
Ranch Hand

Joined: Feb 28, 2013
Posts: 226

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.

OCAJP 7, OCPJP 7
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2052
    
  22

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)
 
 
subject: RunnableFuture when would it be a good case to use it?
 
Similar Threads
How can i get completion status of runnable object which is dispatched to executor class?
Runnable
Inheritance is a waste
Marker InterFace...
Future object with Runnable interface