001/* 002 * Copyright (C) 2008 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 */ 014 015package com.google.common.util.concurrent; 016 017import static java.lang.Math.min; 018import static java.util.concurrent.TimeUnit.NANOSECONDS; 019 020import com.google.common.annotations.GwtIncompatible; 021import com.google.errorprone.annotations.CanIgnoreReturnValue; 022import java.util.concurrent.Callable; 023import java.util.concurrent.ExecutionException; 024import java.util.concurrent.Executor; 025import java.util.concurrent.FutureTask; 026import java.util.concurrent.TimeUnit; 027import java.util.concurrent.TimeoutException; 028import org.jspecify.annotations.Nullable; 029 030/** 031 * A {@link FutureTask} that also implements the {@link ListenableFuture} interface. Unlike {@code 032 * FutureTask}, {@code ListenableFutureTask} does not provide an overrideable {@link 033 * FutureTask#done() done()} method. For similar functionality, call {@link #addListener}. 034 * 035 * <p>Few users should use this class. It is intended primarily for those who are implementing an 036 * {@code ExecutorService}. Most users should call {@link ListeningExecutorService#submit(Callable) 037 * ListeningExecutorService.submit} on a service obtained from {@link 038 * MoreExecutors#listeningDecorator}. 039 * 040 * @author Sven Mawson 041 * @since 1.0 042 */ 043@GwtIncompatible 044public class ListenableFutureTask<V extends @Nullable Object> extends FutureTask<V> 045 implements ListenableFuture<V> { 046 // TODO(cpovirk): explore ways of making ListenableFutureTask final. There are some valid reasons 047 // such as BoundedQueueExecutorService to allow extends but it would be nice to make it final to 048 // avoid unintended usage. 049 050 // The execution list to hold our listeners. 051 private final ExecutionList executionList = new ExecutionList(); 052 053 /** 054 * Creates a {@code ListenableFutureTask} that will upon running, execute the given {@code 055 * Callable}. 056 * 057 * @param callable the callable task 058 * @since 10.0 059 */ 060 public static <V extends @Nullable Object> ListenableFutureTask<V> create(Callable<V> callable) { 061 return new ListenableFutureTask<>(callable); 062 } 063 064 /** 065 * Creates a {@code ListenableFutureTask} that will upon running, execute the given {@code 066 * Runnable}, and arrange that {@code get} will return the given result on successful completion. 067 * 068 * @param runnable the runnable task 069 * @param result the result to return on successful completion. If you don't need a particular 070 * result, consider using constructions of the form: {@code ListenableFuture<?> f = 071 * ListenableFutureTask.create(runnable, null)} 072 * @since 10.0 073 */ 074 public static <V extends @Nullable Object> ListenableFutureTask<V> create( 075 Runnable runnable, @ParametricNullness V result) { 076 return new ListenableFutureTask<>(runnable, result); 077 } 078 079 ListenableFutureTask(Callable<V> callable) { 080 super(callable); 081 } 082 083 ListenableFutureTask(Runnable runnable, @ParametricNullness V result) { 084 super(runnable, result); 085 } 086 087 @Override 088 public void addListener(Runnable listener, Executor exec) { 089 executionList.add(listener, exec); 090 } 091 092 @CanIgnoreReturnValue 093 @Override 094 @ParametricNullness 095 public V get(long timeout, TimeUnit unit) 096 throws TimeoutException, InterruptedException, ExecutionException { 097 098 long timeoutNanos = unit.toNanos(timeout); 099 if (timeoutNanos <= OverflowAvoidingLockSupport.MAX_NANOSECONDS_THRESHOLD) { 100 return super.get(timeout, unit); 101 } 102 // Waiting 68 years should be enough for any program. 103 return super.get( 104 min(timeoutNanos, OverflowAvoidingLockSupport.MAX_NANOSECONDS_THRESHOLD), NANOSECONDS); 105 } 106 107 /** Internal implementation detail used to invoke the listeners. */ 108 @Override 109 protected void done() { 110 executionList.execute(); 111 } 112}