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