多线程中的Runnable、Callable、Future、FutureTask的作用
2016-12-28 18:50
393 查看
在Java多线程编程中,创建启动一个线程可以继承Thread类,也可以实现Runnable接口,但是想要获取线程的结果,则需要实现Callable接口,获取结果则需要使用Future接口。下面分别介绍这几个接口或者类的实现。
public interface Runnable {
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
*
* @see java.lang.Thread#run()
*/
public abstract void run();
}
public interface Callable<V> {
/**
* Computes a result, or throws an exception if unable to do so.
*
* @return computed result
* @throws Exception if unable to compute a result
*/
V call() throws Exception;
}
直到超时,或者获得任务执行结果。
/**
* @see FutureTask
* @see Executor
* @since 1.5
* @author Doug Lea
* @param <V> The result type returned by this Future's <tt>get</tt> method
*/
public interface Future<V> {
/**
* Attempts to cancel execution of this task. This attempt will
* fail if the task has already completed, has already been cancelled,
* or could not be cancelled for some other reason. If successful,
* and this task has not started when <tt>cancel</tt> is called,
* this task should never run. If the task has already started,
* then the <tt>mayInterruptIfRunning</tt> parameter determines
* whether the thread executing this task should be interrupted in
* an attempt to stop the task. *
*/
boolean cancel(boolean mayInterruptIfRunning);
/**
* Returns <tt>true</tt> if this task was cancelled before it completed
* normally.
*/
boolean isCancelled();
/**
* Returns <tt>true</tt> if this task completed.
*
*/
boolean isDone();
/**
* Waits if necessary for the computation to complete, and then
* retrieves its result.
*
* @return the computed result
*/
V get() throws InterruptedException, ExecutionException;
/**
* Waits if necessary for at most the given time for the computation
* to complete, and then retrieves its result, if available.
*
* @param timeout the maximum time to wait
* @param unit the time unit of the timeout argument
* @return the computed result
*/
V get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException;
}
FutureTask是一个RunnableFuture<V>接口实现,而RunnableFuture<V>实现了Runnable和Future两个接口,同时,它还可以包装
Runnable和Callable接口,由构造函数注入:
public FutureTask(Callable<V> callable) {
if (callable == null)
throw new NullPointerException();
this.callable = callable;
this.state = NEW; // ensure visibility of callable
}
public FutureTask(Runnable runnable, V result) {
this.callable = Executors.callable(runnable, result);
this.state = NEW; // ensure visibility of callable
}
由于FutureTask实现了Runnable接口和Future接口,同时包装了Callable,所以它既可以传递给Thread类来执行,也可以提交给
ExecuteService来执行。并且还可以直接通过get函数来获取执行结果。
Runnable接口
Runnable接口只有一个run方法,其线程执行逻辑在run方法中实现。但是该方法没有返回值,所以不能获得线程执行的结果。其接口定义如下:public interface Runnable {
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
*
* @see java.lang.Thread#run()
*/
public abstract void run();
}
Callable接口
Callable接口和Runnable接口很相似,但是其call方法有返回值,所以可以获取线程执行的返回值。public interface Callable<V> {
/**
* Computes a result, or throws an exception if unable to do so.
*
* @return computed result
* @throws Exception if unable to compute a result
*/
V call() throws Exception;
}
Future接口
Future就是对于具体的Runnable或者Callable任务的执行结果进行取消、查询是否完成、获取结果、设置结果操作。其get方法会阻塞,直到超时,或者获得任务执行结果。
/**
* @see FutureTask
* @see Executor
* @since 1.5
* @author Doug Lea
* @param <V> The result type returned by this Future's <tt>get</tt> method
*/
public interface Future<V> {
/**
* Attempts to cancel execution of this task. This attempt will
* fail if the task has already completed, has already been cancelled,
* or could not be cancelled for some other reason. If successful,
* and this task has not started when <tt>cancel</tt> is called,
* this task should never run. If the task has already started,
* then the <tt>mayInterruptIfRunning</tt> parameter determines
* whether the thread executing this task should be interrupted in
* an attempt to stop the task. *
*/
boolean cancel(boolean mayInterruptIfRunning);
/**
* Returns <tt>true</tt> if this task was cancelled before it completed
* normally.
*/
boolean isCancelled();
/**
* Returns <tt>true</tt> if this task completed.
*
*/
boolean isDone();
/**
* Waits if necessary for the computation to complete, and then
* retrieves its result.
*
* @return the computed result
*/
V get() throws InterruptedException, ExecutionException;
/**
* Waits if necessary for at most the given time for the computation
* to complete, and then retrieves its result, if available.
*
* @param timeout the maximum time to wait
* @param unit the time unit of the timeout argument
* @return the computed result
*/
V get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException;
}
FutureTask
FutureTask是一个RunnableFuture<V>接口实现,而RunnableFuture<V>实现了Runnable和Future两个接口,同时,它还可以包装Runnable和Callable接口,由构造函数注入:
public FutureTask(Callable<V> callable) {
if (callable == null)
throw new NullPointerException();
this.callable = callable;
this.state = NEW; // ensure visibility of callable
}
public FutureTask(Runnable runnable, V result) {
this.callable = Executors.callable(runnable, result);
this.state = NEW; // ensure visibility of callable
}
由于FutureTask实现了Runnable接口和Future接口,同时包装了Callable,所以它既可以传递给Thread类来执行,也可以提交给
ExecuteService来执行。并且还可以直接通过get函数来获取执行结果。
相关文章推荐
- 多线程之Runnable,Callable,Future,FutureTask
- Android(Java)之多线程结果返回——Future 、FutureTask、Callable、Runnable
- Android进阶——多线程系列之Thread、Runnable、Callable、Future、FutureTask
- Java多线程21:多线程下的其他组件之CyclicBarrier、Callable、Future和FutureTask
- Java多线程研究06-带返回值的线程定义接口Callable以及Future,FutureTask的使用
- Java中的Runnable、Callable、Future、FutureTask的区别与示例
- Runnable,Callable,Future,RunnableFuture,FutureTask,ExecutorService的关系
- Thread、Runnable、Callable、Future、FutureTask
- Runnable、Callable、Future、FutureTask
- 理解Android多线程里面三种任务Runnable和Callable和FutureTask的用法
- Java多线程(十七):Callable、Future和FutureTask
- Java中的Runnable、Callable、Future、FutureTask的区别与示例
- 多线程 Callable Runnable 与Future
- 多线程下的其它组件之CyclicBarrier、Callable、Future、FutureTask
- java 多线程(一)---创建线程的三种方式Thread,Runnable,Callable与Future
- Java线程和多线程(十三)——Callable,Future,FutureTask
- java多线程Runnable、Callable、Executor、Future、FutureTask关系解读
- Runnable、Callable、Future、FutureTask
- Java中的Runnable、Callable、Future、FutureTask
- Java中的Runnable、Callable、Future、FutureTask的区别与示例