tags:

views:

324

answers:

1

I really want to create a subclass of FutureTask that has a default no-arg constructor. In particular, I want my subclass to implement the Callable interface and use itself as the callable. This way, users of MyFutureTask can just subclass MyFutureTask instead of having to implement their own callable and pass it to an instance of FutureTask.

Here's the general idea:

public abstract class MyFutureTask<Result> extends FutureTask<Result> implements Callable<Result> {

    public MyFutureTask() {
        super( /*XXX*/ );        
    }

    public abstract Result call() throws Exception;

}

The problem is, what can I put in the XXX? FutureTask requires a Callable, but I can't pass this because java doesn't allow references to this from within a call to super. I can't instantiate a nested class since that's also disallowed.

Is there a clever (or non-clever) way I can do this?

+1  A: 

You could do this using composition and the creation of an abstract class. now you clients are decoupled from the callable interface.

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

public abstract class MyFutureClass<V> implements Callable<V> {

private final FutureTask<V> futureTask;

public MyFutureClass() {
 futureTask = new FutureTask<V>(this);
}

@Override
public V call() throws Exception {
 return myCall();
}

protected abstract V myCall();

public FutureTask<V> getFutureTask() {
 return futureTask;
}
}
MrWhite
Great, thanks guys. Yeah, that would work, although it has the disadvantage of not extending from FutureTask, so I can't pass it in anywhere that expects a FutureTask. So far though it's the best solution I've seen, unfortunately.
Mike