com.google.common.util.concurrent.ForwardingExecutorService Java Examples

The following examples show how to use com.google.common.util.concurrent.ForwardingExecutorService. 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: TestExecutors.java    From ibm-cos-sdk-java with Apache License 2.0 6 votes vote down vote up
public static ExecutorService blocksOnFirstCallFromCallableOfType(final ExecutorService delegate, final Class<? extends Callable> type) {
    return new ForwardingExecutorService() {
        private boolean firstCall = true;

        @Override
        protected ExecutorService delegate() {
            return delegate;
        }

        @Override
        public <T> Future<T> submit(Callable<T> task) {
            if (task.getClass().equals(type) && firstCall) {
                firstCall = false;
                try {
                    return Futures.immediateFuture(task.call());
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
            return super.submit(task);
        }
    };
}