Java 并发编程:Callable+Future+FutureTask详解

Runnable
其中Runnable应该是我们最熟悉的接口,它只有一个run()函数,用于将耗时操作写在其中,该函数没有返回值。然后使用某个线程去执行该runnable即可实现多线程,Thread类在调用start()函数后就是执行的是Runnable的run()函数。Runnable的声明如下 :

public interface Runnable {/** @see java.lang.Thread#run()*/public abstract void run();
}

Callable
Callable与Runnable的功能大致相似,Callable中有一个call()函数,但是call()函数有返回值,而Runnable的run()函数不能将结果返回给客户程序。Callable的声明如下 :

public interface Callable {/*** 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
Executor就是Runnable和Callable的调度容器,Future就是对于具体的Runnable或者Callable任务的执行结果进行

取消、查询是否完成、获取结果、设置结果操作。get方法会阻塞,直到任务返回结果(Future简介)。Future声明如下 :

public interface Future {/*** 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 cancel is called,
  • this task should never run. If the task has already started,
  • then the mayInterruptIfRunning parameter determines
  • whether the thread executing this task should be interrupted in
  • an attempt to stop the task./boolean cancel(boolean mayInterruptIfRunning);/** Returns true if this task was cancelled before it completed
  • normally./boolean isCancelled();/** Returns true 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

public class FutureTask
RunnableFuture实现了Runnbale又实现了Futrue

public interface RunnableFutureextends Runnable, Future {/*** Sets this Future to the result of its computation

  • unless it has been cancelled.*/void run();
    }
    另外FutureTaslk还可以包装Runnable和Callable, 由构造函数注入依赖。public FutureTask(Callable 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}

上面代码块可以看出:Runnable注入会被Executors.callable()函数转换为Callable类型,即FutureTask最终都是执行Callable类型的任务。该适配函数的实现如下 :

public static Callable callable(Runnable task, T result) {if (task == null)throw new NullPointerException();return new RunnableAdapter(task, result);
}

RunnableAdapter适配器

/**

  • A callable that runs given task and returns given result*/static final class RunnableAdapterimplements Callable {final Runnable task;final T result;
    RunnableAdapter(Runnable task, T result) {this.task = task;this.result = result;
    }public T call() {
    task.run();return result;
    }
    }

FutureTask实现Runnable,所以能通过Thread包装执行,

FutureTask实现Runnable,所以能通过提交给ExcecuteService来执行

注:ExecuteService:创建线程池实例对象,其中有submit(Runnable)、submit(Callable)方法

还可以直接通过get()函数获取执行结果,该函数会阻塞,直到结果返回。

因此FutureTask是Future也是Runnable,又是包装了的Callable( 如果是Runnable最终也会被转换为Callable )。

Callable 和 Future接口的区别

1.Callable规定的方法是call(),而Runnable规定的方法是run().
2.Callable的任务执行后可返回值,而Runnable的任务是不能返回值的。
3.call()方法可抛出异常,而run()方法是不能抛出异常的。
4.运行Callable任务可拿到一个Future对象, Future表示异步计算的结果。
5.它提供了检查计算是否完成的方法,以等待计算的完成,并检索计算的结果。
6.通过Future对象可了解任务执行情况,可取消任务的执行,还可获取任务执行的结果。
7.Callable是类似于Runnable的接口,实现Callable接口的类和实现Runnable的类都是可被其它线程执行的任务。

示例:

package com.xzf.callable;import java.util.concurrent.Callable;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;import java.util.concurrent.FutureTask;public class RunnableFutureTask {static ExecutorService executorService = Executors.newSingleThreadExecutor(); //创建一个单线程执行器public static void main(String[] args) {
runnableDemo();
futureDemo();
}/*** new Thread(Runnable arg0).start(); 用Thread()方法开启一个新线程

  • runnable, 无返回值*/static void runnableDemo() {new Thread(new Runnable() {
    public void run() {
    System.out.println(“runnable demo:” + fibc(20)); //有值}

}).start();
}/*** Runnable实现的是void run()方法,无返回值

  • Callable实现的是 V call()方法,并且可以返回执行结果
  • Runnable可以提交给Thread,在包装下直接启动一个线程来执行
  • Callable一般都是提交给ExecuteService来执行*/ static void futureDemo() {try {
    Future result1 = executorService.submit(new Runnable() {public void run() {
    fibc(20);
    }
    });
    System.out.println(“future result from runnable:”+result1.get()); //run()无返回值所以为空,result1.get()方法会阻塞Futureresult2 = executorService.submit(new Callable() {public Integer call() throws Exception {return fibc(20);
    }
    });
    System.out.println(“future result from callable:”+result2.get()www.meimeitu8.com); //call()有返回值,result2.get()方法会阻塞FutureTaskresult3 = new FutureTask(new Callable() {public Integer call() throws Exception {return fibc(20);
    }
    });
    executorService.submit(result3);
    System.out.println(“future result from FutureTask:” + result3.get()); //call()有返回值,result3.get()方法会阻塞/因为FutureTask实现了Runnable,因此它既可以通过Thread包装来直接执行,也可以提交给ExecuteService来执行/FutureTaskresult4 = new FutureTask(new Runnable() {public void run() {
    fibc(20);
    }
    },fibc(20));
    executorService.submit(result4);
    System.out.println(“future result from executeService FutureTask :” + result4.get()); //call()有返回值,result3.get()方法会阻塞//这里解释一下什么FutureTask实现了Runnable结果不为null,这就用到FutureTask对Runnable的包装,所以Runnable注入会被Executors.callable()函数转换成Callable类型FutureTaskresult5 = new FutureTask(new Runnable() {public void run() {
    fibc(20);
    }
    },fibc(20));new Thread(result5).start();
    System.out.println(“future result from Thread FutureTask :” + result5.get()); //call()有返回值,result5.get()方法会阻塞} catch (Exception e) {
    e.printStackTrace();
    }finally {
    executorService.shutdown();
    }
    }static int fibc(int num) {if (num0) {return 0;
    }if (num
    1) {return 1;
    }return fibc(num-1) + fibc(num-2);
    }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值