com.google.common.util.concurrent.ForwardingListenableFuture.SimpleForwardingListenableFuture Java Examples

The following examples show how to use com.google.common.util.concurrent.ForwardingListenableFuture.SimpleForwardingListenableFuture. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DeadlockDetectingListeningExecutorService.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
private <T> @NonNull ListenableFuture<T> wrapListenableFuture(final @NonNull ListenableFuture<T> delegate) {
    /*
     * This creates a forwarding Future that overrides calls to get(...) to check, via the
     * ThreadLocal, if the caller is doing a blocking call on a thread from this executor. If
     * so, we detect this as a deadlock and throw an ExecutionException even though it may not
     * be a deadlock if there are more than 1 thread in the pool. Either way, there's bad
     * practice somewhere, either on the client side for doing a blocking call or in the
     * framework's threading model.
     */
    return new SimpleForwardingListenableFuture<T>(delegate) {
        @Override
        public T get() throws InterruptedException, ExecutionException {
            checkDeadLockDetectorTL();
            return super.get();
        }

        @Override
        public T get(final long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException,
                TimeoutException {
            checkDeadLockDetectorTL();
            return super.get(timeout, unit);
        }

        void checkDeadLockDetectorTL() throws ExecutionException {
            if (deadlockDetector.get().isSet()) {
                throw new ExecutionException("A potential deadlock was detected.",
                        deadlockExceptionFunction.get());
            }
        }
    };
}