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