Java 自定义未来对象

Java 自定义未来对象,java,multithreading,concurrency,threadpool,Java,Multithreading,Concurrency,Threadpool,我想创建自定义的未来对象 下面的代码工作正常 ThreadPoolExecutor mExecutor; Future<?> f = mExecutor.submit(new DownloadRunnable(task, itemId)); ThreadPoolExecutor-mExecutor; Future f=mExecutor.submit(新下载可运行(任务,项目ID)); 我想获取submit的返回值,并将其分配给MyFuture对象,并进行其他调用。 我做了以下更

我想创建自定义的未来对象

下面的代码工作正常

ThreadPoolExecutor mExecutor;
Future<?> f = mExecutor.submit(new DownloadRunnable(task, itemId));
ThreadPoolExecutor-mExecutor;
Future f=mExecutor.submit(新下载可运行(任务,项目ID));
我想获取submit的返回值,并将其分配给MyFuture对象,并进行其他调用。 我做了以下更改,并获得了一个强制转换异常。。。有什么建议吗

ThreadPoolExecutor mExecutor;
// cast exception
MyFuture<?> f = (MyFuture<?>) mExecutor.submit(new DownloadRunnable(task, itemId));
f.setVer(true);


public class MyFuture<?> implements Future<?> {
    Boolean myVar;

    public void setVar(Boolean v) {
        ...
    }
}
ThreadPoolExecutor-mExecutor;
//例外情况
MyFuture f=(MyFuture)mExecutor.submit(newdownloadrunnable(task,itemId));
f、 setVer(真);
公共类MyFuture实现了Future{
布尔myVar;
公共void setVar(布尔值v){
...
}
}

好的
Future
是一个接口,您应该像这样编写它:

public class MuFuture<T> implements Future<T> {

}
公共类MuFuture实现Future{
}
然后我希望代码能起作用:

MyFuture<?> f = (MyFuture<?>) mExecutor.submit(new DownloadRunnable(task, itemId));
f.setVer(true);
MyFuture f=(MyFuture)mExecutor.submit(新下载的runnable(task,itemId));
f、 setVer(真);

您可以通过传递
Future

公共类MyFuture扩展了Future
{
布尔myVar;
未来未来;
我的未来(未来未来)
{
this.fut=fut;
}
公共void setVar(布尔值v)
{
...
}
}
那么下面的一行呢

  MyFuture<?> f = (MyFuture<?>) mExecutor.submit(new DownloadRunnable(task, itemId));
MyFuture f=(MyFuture)mExecutor.submit(新下载的runnable(task,itemId));
变成

  MyFuture<?> f = new MyFuture<?>(mExecutor.submit(new DownloadRunnable(task, itemId)));
MyFuture f=newmyfuture(mExecutor.submit(newdownloadrunnable(task,itemId));

为什么要自定义
未来
?执行器返回自己的未来类型。为什么你认为你可以将它强制转换到你自己的类中?问题来自submit,它只返回一个
future
I编写的错误扩展,它的实现。我不想实现将来的接口,我想实现ThreadPoolExecutor实现的接口并对其进行扩展。
  MyFuture<?> f = new MyFuture<?>(mExecutor.submit(new DownloadRunnable(task, itemId)));