title: ListenableFuture, FutureCallback and timeouts
body: <p>Based on the examples of guava I've seen I've been looking for elegant solutions to my problem. Specifically, I like the way <a href="https://github.com/google/guava/blob/v23.0/guava/src/com/google/common/util/concurrent/Futures.java#L1298" rel="nofollow noreferrer"><code>Futures.addCallback(ListenableFuture, FutureCallback)</code> </a> works, but I'd like to be able to set a timeout on the length of time that can expire before the FutureCallback is invoked. Optimally It would be nice if breaching the timeout just caused an the failure condition of FutureCallback to be called.</p>
  
  <p>Does Guava have something like this already? Is it just not recommended to try to couple timeouts with the callbacks? </p>
  
  <p>EDIT: Including example of the code that led me to this point. Obviously, I stripped out the meaningful bits to get a minimum example.</p>
  
  <pre><code>@Test
  public void testFuture()
  {
      Callable&lt;Boolean&gt; callable = new Callable&lt;Boolean&gt;()
      {
  
          @Override
          public Boolean call() throws Exception
          {
              while(true);
          }
      };
  
      ListenableFuture&lt;Boolean&gt; callableFuture = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor()).submit(callable);
  
      Futures.addCallback(callableFuture, new FutureCallback&lt;Boolean&gt;()
      {
  
          @Override
          public void onFailure(Throwable arg0)
          {
              System.out.println("onFailure:"+arg0); 
          }
  
          @Override
          public void onSuccess(Boolean arg0)
          {
              System.out.println("onSuccess:"+arg0);
          }
      }); 
  
      try
      {
          callableFuture.get(1000, TimeUnit.MILLISECONDS);
      }catch(Throwable t)
      {
          System.out.println("catch:"+t);
      }
  }
  </code></pre>
  
  <p>This code will only print <code>catch:java.util.concurrent.TimeoutException</code>. </p>