Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.facebook.presto.util;
 
 
 
 
 import static com.google.common.base.Preconditions.checkNotNull;
 
 public final class MoreFutures
 {
     private MoreFutures()
     {
     }
 
     public static <T> T tryGetUnchecked(Future<T> future)
     {
         checkNotNull(future"future is null");
         if (!future.isDone()) {
             return null;
         }
 
         try {
             return future.get(0, .);
         }
         catch (InterruptedException e) {
             Thread.currentThread().interrupt();
             throw Throwables.propagate(e);
         }
         catch (ExecutionException e) {
             Throwable cause = e.getCause();
             if (cause == null) {
                 cause = e;
             }
             throw Throwables.propagate(cause);
         }
         catch (TimeoutException e) {
             // this mean that isDone() does not agree with get()
             // this should not happen for reasonable implementations of Future
             throw Throwables.propagate(e);
         }
     }
 
     public static <T> ListenableFuture<T> addTimeout(final ListenableFuture<T> futurefinal Callable<T> timeoutTaskDuration timeoutScheduledExecutorService executorService)
     {
         // if the future is already complete, just return it
         if (future.isDone()) {
             return future;
         }
 
         // wrap the future, so we can set the result directly
         final SettableFuture<T> settableFuture = SettableFuture.create();
 
         // schedule a task to complete the future when the time expires
         final ScheduledFuture<?> timeoutTaskFuture = executorService.schedule(new TimeoutFutureTask<>(settableFuturetimeoutTaskfuture), timeout.toMillis(), .);
 
         // add a listener to the core future, which simply updates the settable future
         Futures.addCallback(futurenew FutureCallback<T>() {
             @Override
             public void onSuccess(@Nullable T result)
             {
                 settableFuture.set(result);
                 timeoutTaskFuture.cancel(true);
             }
 
             @Override
             public void onFailure(Throwable t)
             {
                 settableFuture.setException(t);
                 timeoutTaskFuture.cancel(true);
             }
         }, executorService);
 
        return settableFuture;
    }
    private static class TimeoutFutureTask<T>
            implements Runnable
    {
        private final SettableFuture<T> settableFuture;
        private final Callable<T> timeoutTask;
        private final WeakReference<Future<T>> futureReference;
        public TimeoutFutureTask(SettableFuture<T> settableFutureCallable<T> timeoutTaskListenableFuture<T> future)
        {
            this. = settableFuture;
            this. = timeoutTask;
            // the scheduled executor can hold on to the timeout task for a long time, and
            // the future can reference large expensive objects.  Since we are only interested
            // in canceling this future on a timeout, only hold a weak reference to the future
            this. = new WeakReference<Future<T>>(future);
        }
        @Override
        public void run()
        {
            if (.isDone()) {
                return;
            }
            // run the timeout task and set the result into the future
            try {
                T result = .call();
                .set(result);
            }
            catch (Throwable t) {
                .setException(t);
            }
            // cancel the original future, if it still exists
            Future<T> future = .get();
            if (future != null) {
                future.cancel(true);
            }
        }
    }
New to GrepCode? Check out our FAQ X