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.common.annotations.J2ktIncompatible; 022import com.google.errorprone.annotations.CanIgnoreReturnValue; 023import java.util.concurrent.Callable; 024import java.util.concurrent.ExecutionException; 025import java.util.concurrent.Executor; 026import java.util.concurrent.FutureTask; 027import java.util.concurrent.TimeUnit; 028import java.util.concurrent.TimeoutException; 029import org.jspecify.annotations.Nullable; 030 031/** 032 * A {@link FutureTask} that also implements the {@link ListenableFuture} interface. Unlike {@code 033 * FutureTask}, {@code ListenableFutureTask} does not provide an overrideable {@link 034 * FutureTask#done() done()} method. For similar functionality, call {@link #addListener}. 035 * 036 * <p>Few users should use this class. It is intended primarily for those who are implementing an 037 * {@code ExecutorService}. Most users should call {@link ListeningExecutorService#submit(Callable) 038 * ListeningExecutorService.submit} on a service obtained from {@link 039 * MoreExecutors#listeningDecorator}. 040 * 041 * @author Sven Mawson 042 * @since 1.0 043 */ 044@J2ktIncompatible 045@GwtIncompatible 046public class ListenableFutureTask<V extends @Nullable Object> extends FutureTask<V> 047 implements ListenableFuture<V> { 048 // TODO(cpovirk): explore ways of making ListenableFutureTask final. There are some valid reasons 049 // such as BoundedQueueExecutorService to allow extends but it would be nice to make it final to 050 // avoid unintended usage. 051 052 // The execution list to hold our listeners. 053 private final ExecutionList executionList = new ExecutionList(); 054 055 /** 056 * Creates a {@code ListenableFutureTask} that will upon running, execute the given {@code 057 * Callable}. 058 * 059 * @param callable the callable task 060 * @since 10.0 061 */ 062 public static <V extends @Nullable Object> ListenableFutureTask<V> create(Callable<V> callable) { 063 return new ListenableFutureTask<>(callable); 064 } 065 066 /** 067 * Creates a {@code ListenableFutureTask} that will upon running, execute the given {@code 068 * Runnable}, and arrange that {@code get} will return the given result on successful completion. 069 * 070 * @param runnable the runnable task 071 * @param result the result to return on successful completion. If you don't need a particular 072 * result, consider using constructions of the form: {@code ListenableFuture<?> f = 073 * ListenableFutureTask.create(runnable, null)} 074 * @since 10.0 075 */ 076 public static <V extends @Nullable Object> ListenableFutureTask<V> create( 077 Runnable runnable, @ParametricNullness V result) { 078 return new ListenableFutureTask<>(runnable, result); 079 } 080 081 ListenableFutureTask(Callable<V> callable) { 082 super(callable); 083 } 084 085 ListenableFutureTask(Runnable runnable, @ParametricNullness V result) { 086 super(runnable, result); 087 } 088 089 @Override 090 public void addListener(Runnable listener, Executor exec) { 091 executionList.add(listener, exec); 092 } 093 094 @CanIgnoreReturnValue 095 @Override 096 @ParametricNullness 097 public V get(long timeout, TimeUnit unit) 098 throws TimeoutException, InterruptedException, ExecutionException { 099 100 long timeoutNanos = unit.toNanos(timeout); 101 if (timeoutNanos <= OverflowAvoidingLockSupport.MAX_NANOSECONDS_THRESHOLD) { 102 return super.get(timeout, unit); 103 } 104 // Waiting 68 years should be enough for any program. 105 return super.get( 106 min(timeoutNanos, OverflowAvoidingLockSupport.MAX_NANOSECONDS_THRESHOLD), NANOSECONDS); 107 } 108 109 /** Internal implementation detail used to invoke the listeners. */ 110 @Override 111 protected void done() { 112 executionList.execute(); 113 } 114}