public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
ArrayBlockingQueue<Object> localRQ = resultQueue;
if (state.get() == CANCELED)
throw new CancellationException();
if (localRQ == null && state.get() == ADDED)
throw new IllegalStateException("Tried to get result twice");
Object r = localRQ.poll(timeout, unit);
// could have been canceled while waiting
if (state.get() == CANCELED) {
if (r != null)
throw new IllegalStateException("Nothing should have been added when in canceled state");
throw new CancellationException();
}
if (r == null)
throw new TimeoutException();