Callable,Future,FutureTask

1.概念定义

2.实现例子

3.总结

1.概念定义

1.Callable

Callable是一个接口,效果类似Runnable接口。实现该接口,然后,耗时操作在call()方法中执行。与Runnable接口不同的是,call方法需要返回执行的结果。

public interface Callable<V>{
  V call() throws Exception;
}

public interface Runnable{
  void run();
}

2.Future

A Future represents the result of an asynchronous(异步的) computation. Methods are provided to check if the computation is complete, to wait for its completion, and to retrieve the result of the computation. The result can only be retrieved using method get when the computation has completed, blocking if necessary until it is ready. Cancellation is performed by the cancel method. Additional methods are provided to determine if the task completed normally or was cancelled. Once a computation has completed, the computation cannot be cancelled. If you would like to use a Future for the sake of cancellability but not provide a usable result, you can declare types of the form Future<?> and return null as a result of the underlying task.

Future是一个接口,实现这个接口的对象具备的特征,可以取消任务,获取任务的结果,判断任务的执行状态。Future就是对具体的Callable任务或者Runnable任务执行取消,获取结果,获取当前状态。

public interface Future<V> {

   boolean cancel(boolean mayInterruptIfRunning);
   boolean isCancelled();
   boolean isDone();
   V get() throws InterruptedException, ExecutionException;
   V get(long timeout, TimeUnit unit)
        throws InterruptedException, ExecutionException, TimeoutException;
}

3.FutureTask

A cancellable asynchronous computation. This class provides a base implementation of Future, with methods to start and cancel a computation, query to see if the computation is complete, and retrieve the result of the computation. The result can only be retrieved when the computation has completed; the get methods will block if the computation has not yet completed. Once the computation has completed, the computation cannot be restarted or cancelled (unless the computation is invoked using runAndReset()).

A FutureTask can be used to wrap a Callable or Runnable object. Because FutureTask implements Runnable, a FutureTask can be submitted to an Executor for execution.

In addition to serving as a standalone class, this class provides protected functionality that may be useful when creating customized task classes.

FutureTask实现了Future接口,实现了Runnable接口。那么,它就是一个Runnable对象,Future对象。它的run方法就是执行耗时的操作。

应用:

//创建FutureTask对象,传入Callable对象;Callable对象,耗时操作是放在call方法里面的
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class FutureTaskExample {

       public static void main(String[] args) {

             Callable<String> worker = new Callable<String>() {

                    @Override
                    public String call() throws Exception {
                           // TODO Auto-generated method stub
                           System.out.println("do computation");
                           return "computation result";
                    }
             } ;

             FutureTask<String> future = new FutureTask<String>(worker) {
                    @Override
                    protected void  done() {

                       System.out.println("Done task");
                    }
             };

             new Thread(future) {

                    @Override
                    public void run() {
                           future.run();
                    }

             }.start();

             try {
                    System.out.println("computation:"+future.get());
             } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
             } catch (ExecutionException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
             }
       }
}

调用FutureTask的run方法就触发执行,可以查看FutureTask的源码得到解释:调用run,会导致调用sync.innerRun();而innerRun会调用传入的Callable对象的call方法:

public class FutureTask<V> implements RunnableFuture<V> {

    public FutureTask(Callable<V> callable) {
        if (callable == null)
            throw new NullPointerException();
        sync = new Sync(callable);
    }

.....
    // The following (duplicated) doc comment can be removed once
    //
    // 6270645: Javadoc comments should be inherited from most derived
    //          superinterface or superclass
    // is fixed.
    /**
     * Sets this Future to the result of its computation
     * unless it has been cancelled.
     */
    public void run() {
        sync.innerRun();
    }

    /**
     * Synchronization control for FutureTask. Note that this must be
     * a non-static inner class in order to invoke the protected
     * <tt>done</tt> method. For clarity, all inner class support
     * methods are same as outer, prefixed with "inner".
     *
     * Uses AQS sync state to represent run status
     */
    private final class Sync extends AbstractQueuedSynchronizer {

           void innerRun() {
            if (!compareAndSetState(READY, RUNNING))
                return;

            runner = Thread.currentThread();
            if (getState() == RUNNING) { // recheck after setting thread
                V result;
                try {
                    result = callable.call();
                } catch (Throwable ex) {
                    setException(ex);
                    return;
                }
                set(result);
            } else {
                releaseShared(0); // cancel
            }
        }

    }

}

2.实现例子

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class FutureTaskExample {

       public static void main(String[] args) {

             Callable<String> worker = new Callable<String>() {

                    @Override
                    public String call() throws Exception {
                           // TODO Auto-generated method stub
                           System.out.println("do computation");
                           return "computation result";
                    }
             } ;

             FutureTask<String> future = new FutureTask<String>(worker) {
                    @Override
                    protected void  done() {

                       System.out.println("Done task");
                    }
             };

             new Thread(future) {

                    @Override
                    public void run() {
                           future.run();
                    }

             }.start();

             try {
                    System.out.println("computation:"+future.get());
             } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
             } catch (ExecutionException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
             }
       }
}

3.总结

1.FutureTask就是一个Runnable对象,也是一个Future对象。

2.使用FutureTask要使用Callable对象

3.最终的耗时操作,计算是在Callable对象的call方法中执行

4.使用FutureTask是为了获得Future接口定义的特性,可以获得一个任务的执行状态,执行结果,可以取消任务的执行。这都是Runnable对象不具备的特性,Thread对象也不具备这些特性。

时间: 2024-10-13 18:34:08

Callable,Future,FutureTask的相关文章

java 并发runable,callable,future,futureTask

转载自:http://www.cnblogs.com/dolphin0520/p/3949310.html package future_call; import java.util.concurrent.Callable; /** * Created by luozhitao on 2017/8/10. */ public class Task implements Callable<Integer> { // @Override public Integer call() throws E

12 Callable &amp; Future &amp; FutureTask

创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable接口. 这2种方式都有一个缺陷就是:在执行完任务之后无法获取执行结果. 如果需要获取执行结果,就必须通过共享变量或者使用线程通信的方式来达到效果,这样使用起来就比较麻烦. 而自从Java 1.5开始,就提供了Callable和Future,通过它们可以在任务执行完毕之后得到任务执行结果. 今天我们就来讨论一下Callable.Future和FutureTask三个类的使用方法. 1 Callable与Runnable

Java 并发编程——Callable+Future+FutureTask

项目中经常有些任务需要异步(提交到线程池中)去执行,而主线程往往需要知道异步执行产生的结果,这时我们要怎么做呢?用runnable是无法实现的,我们需要用callable实现. import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Ex

java多线程Callable,Future,FutureTask

我们平时接触到的多线程Thread,Runnable,这两种方式不能返回线程执行后的结果. Callable和Future,前者产生结果,后者拿到结果. Callable和Future 推荐一篇不错的博客:http://blog.csdn.net/ghsau/article/details/7451464 public class GreyStartServlet extends HttpServlet { @Override public void init() throws ServletE

Callable/Future

Callable 和 Future 是比较有趣的一对组合.当我们需要获取线程的执行结果时,就需要用到它们.Callable用于产生结果,Future用于获取结果. 第1部分 Callable Callable 是一个接口,它只包含一个call()方法.Callable是一个返回结果并且可能抛出异常的任务. 为了便于理解,我们可以将Callable比作一个Runnable接口,而Callable的call()方法则类似于Runnable的run()方法. Callable的源码如下: public

Callable, Runnable, Future, FutureTask

Java并发编程之Callable, Runnable, Future, FutureTask Java中存在Callable, Runnable, Future, FutureTask这几个与线程相关的类或接口, 下面来了解一下它们的作用和区别. 一.Callable和Runnable Callable和Runnable类似, 实现Callable和Runnable接口的类都是可以被其他线程运行的任务, Callable和Runnable主要有以下几点区别: (1). Callable中声明的

java多线程编程之Future/FutureTask和Callable

有这样一种场景,用多线程发送数据到某个服务器,需要知道各个线程是否都发送成功,等所有线程都发送完成才能继续下一轮计算和发送.如果用传统的多线程方式,就需要启动多个线程,然后在每个线程中分别发送数据,外部通过某种方式等待各个线程全部都发送完成,再进行后面的计算等流程.这种实现方式的代码会比较臃肿,在java中提供了一种Callable+Future的方法,可以将异步的多线程调用变为同步方式. Callable 在java的多线程编程中,有Thread和Runnable两种方式来新建线程,其中Run

java Future FutureTask 并发操作

1.1 综述 创建线程有两种方式:extends Thread || inplements Runable,但是这两种方式都有一个缺陷,就是执行完任务后不能获取执行结果. 如果非要获取到执行结果的话,就必须通过共享变量或者是线程通信的方法来达到效果,使用较为麻烦,为了解决这种问题,java提供了Callable Future ,通过他们可以再任务执行完毕后得到任务执行结果. 1.2 Runable Callable 对比 java.lang.Runable是一个接口,声明了run()方法: pu

多线程-Callable&amp;Future

Callable和Future出现的原因 创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable接口. 这2种方式都有一个缺陷就是:在执行完任务之后无法获取执行结果. 如果需要获取执行结果,就必须通过共享变量或者使用线程通信的方式来达到效果,这样使用起来就比较麻烦. 而自从Java 1.5开始,就提供了Callable和Future,通过它们可以在任务执行完毕之后得到任务执行结果. Callable和Future介绍 Callable接口代表一段可以调用并返回结果的代码