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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With