多线程中的Runnable、Callable、Future、FutureTask的作用

在Java多线程编程中,创建启动一个线程可以继承Thread类,也可以实现Runnable接口,但是想要获取线程的结果,则需要实现Callable接口,获取结果则需要使用Future接口。下面分别介绍这几个接口或者类的实现。

Runnable接口

Runnable接口只有一个run方法,其线程执行逻辑在run方法中实现。但是该方法没有返回值,所以不能获得线程执行的结果。其接口定义如下:


  1. public interface Runnable {  
  2.     /** 
  3.      * When an object implementing interface <code>Runnable</code> is used 
  4.      * to create a thread, starting the thread causes the object's 
  5.      * <code>run</code> method to be called in that separately executing 
  6.      * thread. 
  7.      * <p> 
  8.      * 
  9.      * @see     java.lang.Thread#run() 
  10.      */  
  11.     public abstract void run();  
  12. }  

Callable接口

Callable接口和Runnable接口很相似,但是其call方法有返回值,所以可以获取线程执行的返回值。

  1. public interface Callable<V> {  
  2.     /** 
  3.      * Computes a result, or throws an exception if unable to do so. 
  4.      * 
  5.      * @return computed result 
  6.      * @throws Exception if unable to compute a result 
  7.      */  
  8.     V call() throws Exception;  
  9. }  

Future接口

Future就是对于具体的Runnable或者Callable任务的执行结果进行取消、查询是否完成、获取结果、设置结果操作。其get方法会阻塞,

直到超时,或者获得任务执行结果。

  1. /** 
  2. * @see FutureTask 
  3.  * @see Executor 
  4.  * @since 1.5 
  5.  * @author Doug Lea 
  6.  * @param <V> The result type returned by this Future's <tt>get</tt> method 
  7.  */  
  8. public interface Future<V> {  
  9.   
  10.     /** 
  11.      * Attempts to cancel execution of this task.  This attempt will 
  12.      * fail if the task has already completed, has already been cancelled, 
  13.      * or could not be cancelled for some other reason. If successful, 
  14.      * and this task has not started when <tt>cancel</tt> is called, 
  15.      * this task should never run.  If the task has already started, 
  16.      * then the <tt>mayInterruptIfRunning</tt> parameter determines 
  17.      * whether the thread executing this task should be interrupted in 
  18.      * an attempt to stop the task.     * 
  19.      */  
  20.     boolean cancel(boolean mayInterruptIfRunning);  
  21.   
  22.     /** 
  23.      * Returns <tt>true</tt> if this task was cancelled before it completed 
  24.      * normally. 
  25.      */  
  26.     boolean isCancelled();  
  27.   
  28.     /** 
  29.      * Returns <tt>true</tt> if this task completed. 
  30.      * 
  31.      */  
  32.     boolean isDone();  
  33.   
  34.     /** 
  35.      * Waits if necessary for the computation to complete, and then 
  36.      * retrieves its result. 
  37.      * 
  38.      * @return the computed result 
  39.      */  
  40.     V get() throws InterruptedException, ExecutionException;  
  41.   
  42.     /** 
  43.      * Waits if necessary for at most the given time for the computation 
  44.      * to complete, and then retrieves its result, if available. 
  45.      * 
  46.      * @param timeout the maximum time to wait 
  47.      * @param unit the time unit of the timeout argument 
  48.      * @return the computed result 
  49.      */  
  50.     V get(long timeout, TimeUnit unit)  
  51.         throws InterruptedException, ExecutionException, TimeoutException;  


FutureTask


FutureTask是一个RunnableFuture<V>接口实现,而RunnableFuture<V>实现了Runnable和Future两个接口,同时,它还可以包装

Runnable和Callable接口,由构造函数注入:

  1. public FutureTask(Callable<V> callable) {  
  2.     if (callable == null)  
  3.         throw new NullPointerException();  
  4.     this.callable = callable;  
  5.     this.state = NEW;       // ensure visibility of callable  
  6. }  
  7.   
  8. public FutureTask(Runnable runnable, V result) {  
  9.     this.callable = Executors.callable(runnable, result);  
  10.     this.state = NEW;       // ensure visibility of callable  
  11. }  

由于FutureTask实现了Runnable接口和Future接口,同时包装了Callable,所以它既可以传递给Thread类来执行,也可以提交给

ExecuteService来执行。并且还可以直接通过get函数来获取执行结果。




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值