[concurrency-interest] ImmediateFuture

Gregg Wonderly gregg at cytetech.com
Wed Jun 17 13:14:09 EDT 2009


Practically, the FutureTask returned below, must be executed as well, or all 
get() request will block and never return.  I find it much better to just do the 
whole job and avoid any synchronization.

public static <T> Future<T> futureFor( final T value ) {
	return new Future<T>() {
		public boolean cancel( boolean intr ) {
			return false;
		}
		public T get() {
			return value;
		}
		public T get( long to, TimeUnit unit ) {
			return get();
		}
		public boolean isCancelled() {
			return false;
		}
		public boolean isDone() {
			return true;
		}
	};
}

And yes, it would be nice to have a class in the JDK that was such an 
implementation of Future.

Gregg Wonderly

Jim Andreou wrote:
> One could use something like this, to avoid introducing a new type:
> 
> private static final Runnable emptyRunnable = new Runnable() { public 
> void run() { } };
> public static <T> Future<T> futureFor(T value) { 
>     return new FutureTask<T>(emptyRunnable, value);
> }
> 
> 2009/6/17 Joe Kearney <Joe.Kearney at morganstanley.com 
> <mailto:Joe.Kearney at morganstanley.com>>
> 
>     I had to write one of these, I called it ConstantFuture. Fairly
>     trivial implementation, but would be nice to have on hand.
> 
>     2009/6/17 Mark Thornton <mthornton at optrak.co.uk
>     <mailto:mthornton at optrak.co.uk>>
> 
>         For test purposes I created an ImmediateFuture class which
>         implements Future but where the value already exists. Am I the
>         only person to have found the need to do this or should
>         something like it be considered for more general use?
> 
> 
>         Mark Thornton
> 
>         _______________________________________________
>         Concurrency-interest mailing list
>         Concurrency-interest at cs.oswego.edu
>         <mailto:Concurrency-interest at cs.oswego.edu>
>         http://cs.oswego.edu/mailman/listinfo/concurrency-interest
> 
> 
> 
>     _______________________________________________
>     Concurrency-interest mailing list
>     Concurrency-interest at cs.oswego.edu
>     <mailto:Concurrency-interest at cs.oswego.edu>
>     http://cs.oswego.edu/mailman/listinfo/concurrency-interest
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest



More information about the Concurrency-interest mailing list