Checks the state of `Future` on` get (0, TimeUnit.Microseconds) `is a good idea?

I have a Future , and I want to find out what kind of condition. I meant 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; } 

It looks like FutureTask will try to hold the lock, and if it can get the lock, the status of the Future will be checked. So this is a good idea.

Are there any pitfalls that I'm missing here?

+6
source share
1 answer

As in the comments , just use Future.isDone to check the Future.isDone status. However, you still need to call get() to determine if it completed successfully by checking for exceptions.

+3
source

All Articles