001/* 002 * Copyright (C) 2011 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 com.google.common.annotations.GwtIncompatible; 018import com.google.errorprone.annotations.CanIgnoreReturnValue; 019import com.google.errorprone.annotations.CheckReturnValue; 020import java.util.concurrent.AbstractExecutorService; 021import java.util.concurrent.Callable; 022import java.util.concurrent.RunnableFuture; 023import org.jspecify.annotations.Nullable; 024 025/** 026 * Abstract {@link ListeningExecutorService} implementation that creates {@link ListenableFuture} 027 * instances for each {@link Runnable} and {@link Callable} submitted to it. These tasks are run 028 * with the abstract {@link #execute execute(Runnable)} method. 029 * 030 * <p>In addition to {@link #execute}, subclasses must implement all methods related to shutdown and 031 * termination. 032 * 033 * @author Chris Povirk 034 * @since 14.0 035 */ 036@CheckReturnValue 037@GwtIncompatible 038public abstract class AbstractListeningExecutorService extends AbstractExecutorService 039 implements ListeningExecutorService { 040 /** Constructor for use by subclasses. */ 041 public AbstractListeningExecutorService() {} 042 043 /** 044 * @since 19.0 (present with return type {@code ListenableFutureTask} since 14.0) 045 */ 046 @CanIgnoreReturnValue // TODO(kak): consider removing this 047 @Override 048 protected final <T extends @Nullable Object> RunnableFuture<T> newTaskFor( 049 Runnable runnable, @ParametricNullness T value) { 050 return TrustedListenableFutureTask.create(runnable, value); 051 } 052 053 /** 054 * @since 19.0 (present with return type {@code ListenableFutureTask} since 14.0) 055 */ 056 @CanIgnoreReturnValue // TODO(kak): consider removing this 057 @Override 058 protected final <T extends @Nullable Object> RunnableFuture<T> newTaskFor(Callable<T> callable) { 059 return TrustedListenableFutureTask.create(callable); 060 } 061 062 @CanIgnoreReturnValue // TODO(kak): consider removing this 063 @Override 064 public ListenableFuture<?> submit(Runnable task) { 065 return (ListenableFuture<?>) super.submit(task); 066 } 067 068 @CanIgnoreReturnValue // TODO(kak): consider removing this 069 @Override 070 public <T extends @Nullable Object> ListenableFuture<T> submit( 071 Runnable task, @ParametricNullness T result) { 072 return (ListenableFuture<T>) super.submit(task, result); 073 } 074 075 @CanIgnoreReturnValue // TODO(kak): consider removing this 076 @Override 077 public <T extends @Nullable Object> ListenableFuture<T> submit(Callable<T> task) { 078 return (ListenableFuture<T>) super.submit(task); 079 } 080}