FutureTask that implements Callable

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 it as a callable. This way, MyFutureTask users can simply subclass MyFutureTask instead of implementing their own callers and passing them to a FutureTask instance.

Here's a 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 add in XXX? FutureTask requires Callable, but I can not pass thisbecause java does not allow to refer to thisa call super. I cannot create an instance of a nested class, as this is also forbidden.

Is there a smart (or not smart) way that I can do this?

+3
1

, . .

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;
}
}
+2

Source: https://habr.com/ru/post/1718420/


All Articles