0

我有一种情况,我需要创建一个带有 Callable 的 FutureTask 来检查它的所有者是否已被取消。我的代码如下所示:

public static FutureTask<Result> makeFuture(final Call call, final TaskCompletionCallback completion) {
    return new FutureTask<Result>(new Callable<Result>() {
        @Override
        public Result call() throws Exception {
            Result result = CYLib.doNetworkRequest(call, new CarryOnCallback() {
                @Override
                public boolean shouldCarryOn() {
                    return !FutureTask.isDone();
                }
            });
            return result;
        }
    });
}

基本上doNetworkRequest询问CarryOnCallback它是否应该在操作期间的某些时间继续。我希望此回调查看FutureTask调用的doNetworkRequest是否已取消,这涉及查询实际FutureTask对象。

现在我知道您无法真正访问“this”,因为它尚未构建。但是有没有办法解决这个问题,或者对我的情况有更好的设计?

干杯

编辑:

好的,所以我现在就这样处理。制作了一个自定义的 Callable 和 FutureTask。Callable 包含对 FutureTask 的引用,这可以在创建新的 Callable 后手动设置:

public static MyTask makeMyTask(final Call call, final TaskCompletionCallback completion) {
    MyTask task = null;
    MyTask.InnerCallable innerCallable = new MyTask.InnerCallable(call, completion);
    task = new MyTask(innerCallable);
    innerCallable.setParent(task);
    return task;
}

仅供参考,InnerCallable看起来像这样:

public static class MyTask extends FutureTask<Result> {

    InnerCallable callable;

    public MyTask(InnerCallable callable) {
        super(callable);
        this.callable = callable;
    }

    private static class InnerCallable implements Callable<Result> {

        private final Call call;
        private final TaskCompletionCallback completion;
        private WeakReference<MyTask> parentTask;

        InnerCallable(Call call, TaskCompletionCallback completion) {
            this.call = call;
            this.completion = completion;
        }

        @Override
        public Result call() {
            Result result = CYLib.doNetworkRequest(this.call, new CarryOnCallback() {
                @Override
                public boolean shouldCarryOn() {
                    MyTask task = parentTask.get();
                    return !(task == null || task.isCancelled());
                }
            });

            return result;
        }

        private void setParent(MyTask parentTask) {
            this.parentTask = new WeakReference<MyTask>(parentTask);
        }
    }
}
4

1 回答 1

0

那么,您CYLib.doNetworkRequest()正在另一个线程中工作吗?

private static Map<Call,FutureTask> map=new HashMap();

public static FutureTask<Result> makeFuture(final Call call, final TaskCompletionCallback completion) {
    FutureTask<Result> futureResult = new FutureTask<Result>(new Callable<Result>() {
        @Override
        public Result call() throws Exception {
            Result result = CYLib.doNetworkRequest(call, new CarryOnCallback() {
                @Override
                public boolean shouldCarryOn() {
                    return !map.get(call).isCancelled();
                }
            });
            return result;
        }
    });
    map.put(call,futureResult);
    return futureResult;
}
于 2013-10-27T12:44:20.513 回答