Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is checking `Future` state by `get(0, TimeUnit.Microseconds)` a good idea?

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?

like image 467
HBase Avatar asked Dec 24 '12 23:12

HBase


1 Answers

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.

like image 144
Duncan Jones Avatar answered Sep 28 '22 02:09

Duncan Jones