无法编译FutureTask内部类创建

时间:2016-03-26 11:21:18

标签: java generics concurrency

以下代码无法编译(JDK 1.8.0_40),我无法理解原因。

public abstract class BackgroundThread<T> {
    private final ExecutorService executorService = Executors.newFixedThreadPool(1);
    private final FutureTask<T> task;

    public <T> BackgroundThread() {
        this.task = new FutureTask<T>(new Callable<T>() {
            @Override
            public T call() throws Exception {
                return BackgroundThread.this.perform();
            }
        });
    }

    public abstract T perform() throws Exception;

    public abstract void onDone();

    public abstract void setProgress(int i);

    public final void start() {

    }
}

错误讯息: 错误(17,53)Java不兼容类型:T无法转换为T

1 个答案:

答案 0 :(得分:2)

从构造函数中删除type参数,因为它定义了一个类型变量,其范围与类声明中具有相同名称T的变量完全不同:

public BackgroundThread() {