[concurrency-interest] Receiving notifications from Futures

Manik Surtani manik at jboss.org
Mon May 18 08:38:07 EDT 2009


Hi

I'm creating an API that returns Futures.  One of the things that may  
be useful to callers is to be notified when the Future completes, so  
rather than doing a future.get(), the thread can register a callback  
on the future and proceed with other stuff instead, being notified  
when the future has completed and can then call future.get() to  
retrieve any results.

I have looked at the CompletionService API, and while this does help  
to some degree, it is still not something I can expose to any calling  
code, mainly because calling code does not register Callables on my  
interface.  Instead it calls methods that return Futures.

I'm guessing I cannot be the first person with the need for a  
notifying future - how has this been achieved before?

I was thinking of something like:

public interface  NotifyingFuture<T> extends Future<T> {
    NotifyingFuture<T> notifyOnCompletion(NotifyingListener<T> l);
  }

public interface NotifyingListener<T> {
   void futureCompleted(NotifyingFuture<T> f);
}

so that calling code could do:

Future f =  
myAPI.masyncMethodThatReturnsFuture().notifyOnCompletion(listener);

Cheers
Manik



More information about the Concurrency-interest mailing list