我有一个Future
,我想知道它的状态是什么。我想到的是这样的代码:
try {
// Is that a good idea? Counting on exceptions looks weird.
future.get(0, TimeUnit.MICROSECONDS);
this.status = DONE;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw Throwables.propagate(e);
} catch (ExecutionException e) {
this.status = FAILED;
} catch (TimeoutException e) {
this.status = RUNNING;
} catch (CancellationException e) {
this.status = CANCELED;
}
看起来FutureTask
会尝试持有锁,如果它可以得到锁将检查Future
状态。所以这似乎是个好主意。
有没有我在这里遗漏的陷阱?