What's the difference between Future and FutureTask in Java?
2017-09-21 21:34
543 查看
Since use ExecutorService can submit a Callable task and return a Future, why need to use FutureTask to wrap Callable task and use the method execute? I feel they both do the same thing.
The only difference between Future and RunnableFuture, is the run() method:
A good reason to let the Executor construct the FutureTask for you is to ensure that there is no possible way more than one reference exists to the FutureTask instance. That is, the Executor owns this instance.
https://stackoverflow.com/questions/4956822/whats-the-difference-between-future-and-futuretask-in-java
/** * Returns a <tt>RunnableFuture</tt> for the given callable task. * * @param callable the callable task being wrapped * @return a <tt>RunnableFuture</tt> which when run will call the * underlying callable and which, as a <tt>Future</tt>, will yield * the callable's result as its result and provide for * cancellation of the underlying task. * @since 1.6 */ protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) { return new FutureTask<T>(callable); }
The only difference between Future and RunnableFuture, is the run() method:
/** * A {@link Future} that is {@link Runnable}. Successful execution of * the <tt>run</tt> method causes completion of the <tt>Future</tt> * and allows access to its results. * @see FutureTask * @see Executor * @since 1.6 * @author Doug Lea * @param <V> The result type returned by this Future's <tt>get</tt> method */ public interface RunnableFuture<V> extends Runnable, Future<V> { /** * Sets this Future to the result of its computation * unless it has been cancelled. */ void run(); }
A good reason to let the Executor construct the FutureTask for you is to ensure that there is no possible way more than one reference exists to the FutureTask instance. That is, the Executor owns this instance.
https://stackoverflow.com/questions/4956822/whats-the-difference-between-future-and-futuretask-in-java
相关文章推荐
- What’s the difference between an interface and an abstract class in Java?
- What is the difference between a soft reference and a weak reference in Java?
- what is the difference between static and normal variables in c++
- What is the difference between Views and Materialized Views in Oracle?
- In C++, what’s the difference between an inline function and a macro?
- What is difference between HashMap and Hashtable in Java?
- the difference between final and static in java
- What are the differences between struct and class in C++?
- what is the difference between "isempty"and "isnull"in the qt bool QString::isEmpty () const Retur.
- What is the Difference Between Type and Class in
- what is the difference between definition and declaration in c
- Java-What is the difference between <? super E> and <? extends E>?
- The difference between scheduleAtFixedRate and scheduleWithFixedDelay in JAVA
- What is the difference between BIT and TINYINT in MySQL
- What is the difference between @staticmethod and @classmethod in Python?
- In PHP5, what is the difference between using self and $this? When is each appropriate?
- What is the difference between provider network and self-service network in OpenStack?
- What are the practical differences between SVD and wavelet transforms in data analysis?
- What is the difference between task and thread?
- what is the difference between _source and _all in Elasticsearch