自定义未来对象

阿维兰

我想创建自定义的Future对象。

下面的代码工作正常

ThreadPoolExecutor mExecutor;
Future<?> f = mExecutor.submit(new DownloadRunnable(task, itemId));

我想通过其他调用获取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) {
        ...
    }
}
用户4624062

您可以通过传递一个构造函数 Future<?>

 public class MyFuture<?> extends Future<?> 
{
      Boolean myVar;
      Future<?> fut;
      MyFuture<?>(Future<?> fut)
      {
           this.fut = fut;
      }

      public void setVar(Boolean v) 
      {
          ...
      }
}

所以下面一行

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

变成

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章