11

我想等待两个任务完成然后返回它们的结果,但有时我会收到这个错误。为什么?CancellationException 是从哪里来的?

public class ShouldVoteTask extends AbstractWorkerTask<Void, Void, Boolean> {
    private final int placeID; 
    private final int userID;

    public ShouldVoteTask(final int placeID, final int userID) {
        this.placeID = placeID;
        this.userID = userID;
    }

    @Override
    protected Boolean doInBackground(final Void... params) {
        try {
            // Prepare callables.
            final IsMaxRatingCallable call1 = new IsMaxRatingCallable(placeID);
            final DidVoteCallable call2 = new DidVoteCallable(placeID, userID);         
            final List<Callable<Boolean>> callables = new ArrayList<Callable<Boolean>>();           
            callables.add(call1);
            callables.add(call2);

            // Execute them.
            final ExecutorService service = Executors.newFixedThreadPool(2);            
            final List<Future<Boolean>> futures = service.invokeAll(callables, 5, TimeUnit.SECONDS);

            // Check the result.
            boolean result = true;
            for(final Future<Boolean> future : futures) {
                if(future.get()) {
                    result = false;
                }
            }

            return result;
        } catch (final InterruptedException e) {
            e.printStackTrace();
        } catch (final ExecutionException e) {
            e.printStackTrace();
        }
        return false;
    }
}

private class IsMaxRatingCallable implements Callable<Boolean> {
    private final int placeID;

    public IsMaxRatingCallable(final int placeID) {
        this.placeID = placeID;
    }

    @Override
    public Boolean call() throws Exception {
        return Places.isMaxRating(placeID);         
    }
}

private class DidVoteCallable implements Callable<Boolean> {
    private final int placeID;
    private final int userID;

    public DidVoteCallable(final int placeID, final int userID) {
        this.placeID = placeID;
        this.userID = userID;
    }

    @Override
    public Boolean call() throws Exception {
        return Votes.didVote(placeID, userID);          
    }
}

错误

E/AndroidRuntime(19014): FATAL EXCEPTION: AsyncTask #1
E/AndroidRuntime(19014): java.lang.RuntimeException: An error occured while executing doInBackground()
E/AndroidRuntime(19014):    at android.os.AsyncTask$3.done(AsyncTask.java:200)
E/AndroidRuntime(19014):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
E/AndroidRuntime(19014):    at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
E/AndroidRuntime(19014):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
E/AndroidRuntime(19014):    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
E/AndroidRuntime(19014):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
E/AndroidRuntime(19014):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
E/AndroidRuntime(19014):    at java.lang.Thread.run(Thread.java:1027)
E/AndroidRuntime(19014): Caused by: java.util.concurrent.CancellationException
E/AndroidRuntime(19014):    at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:222)
E/AndroidRuntime(19014):    at java.util.concurrent.FutureTask.get(FutureTask.java:83)
E/AndroidRuntime(19014):    at com.vfa.android.planet.task.ShouldVoteTask.doInBackground(ShouldVoteTask.java:43)
E/AndroidRuntime(19014):    at com.vfa.android.planet.task.ShouldVoteTask.doInBackground(ShouldVoteTask.java:1)
E/AndroidRuntime(19014):    at android.os.AsyncTask$2.call(AsyncTask.java:185)
E/AndroidRuntime(19014):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
E/AndroidRuntime(19014):    ... 4 more
4

1 回答 1

26

您要求执行服务以 5 秒的超时时间执行您的可调用对象。根据javadoc

[在超时结束前] 未完成的任务将被取消

我的猜测是future.get()抛出 aCancellationException因为已经达到超时并且执行程序调用future.cancel().

您可以:

  • 增加超时
  • InterruptedException在您的可调用对象中捕获一个以优雅地处理取消
于 2012-07-17T08:38:07.560 回答