Reputation: 191
I'm having a Future
and I want to find out what is its state. What I had in mind is a code like:
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;
}
Looks like FutureTask
will try hold a lock, and if it can get the lock will check Future
's state. So it seems like a good idea.
Are there pitfalls I'm missing here?
Upvotes: 6
Views: 1906
Reputation: 69410
As suggested in the comments, just use Future.isDone
to check the run status. However you still need to call get()
to determine if it completed successfully, checking for exceptions.
Upvotes: 3