普通的创建线程,一种是直接继承Thread,另外一种就是实现Runnable接口。但是这两种都无法在执行完任务之后获取执行结果,Callable、Future就提供了这样的便利。
 
Future的方法说明:
 
boolean cancel(boolean mayInterruptIfRunning);
     cancel方法用来取消任务,如果取消任务成功则返回true,如果取消任务失败则返回false。
  1. 参数mayInterruptIfRunning表示是否允许取消正在执行却没有执行完毕的任务,如果设置true,则表示可以取消正在执行过程中的任务。
  2. 如果任务已经完成,则无论mayInterruptIfRunning为true还是false,此方法肯定返回false,即如果取消已经完成的任务会返回false;
  3. 如果任务正在执行,若mayInterruptIfRunning设置为true,则返回true,若mayInterruptIfRunning设置为false,则返回false;
  4. 如果任务还没有执行,则无论mayInterruptIfRunning为true还是false,肯定返回true。
boolean isCancelled();
     isCancelled方法表示任务是否被取消成功,如果在任务正常完成前被取消成功,则返回 true。
boolean isDone();
     isDone方法表示任务是否已经完成,若任务完成,则返回true;
V get() throws InterruptedException, ExecutionException;
     get()方法用来获取执行结果,这个方法会产生阻塞,会一直等到任务执行完毕才返回;
V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException;
     get(long timeout, TimeUnit unit)用来获取执行结果,如果在指定时间内,还没获取到结果,就直接返回null。
 
而FutureTask即可以作为Runnable又可以作为Future,这样就既可以用ExecutorService的execute执行任务,也可以用ExecutorService的submit提交任务。
 
例子1:
Callable、Future实现子线程执行任务,并返回结果,主线程等待子线程结果在进行其他逻辑。(多个子线程并行执行任务,主线程做合并处理参见CompletionService用法
 
 import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future; public class CallableAndFuture1 { public static void main(String[] args) {
ExecutorService service = Executors.newSingleThreadExecutor(); Future<Integer> future = service.submit( new Callable<Integer>() { @Override
public Integer call() throws Exception {
System. out.println("子线程" + Thread.currentThread().getName() + "在进行计算");
Thread. sleep(3000); int sum = 0;
for (int i = 0; i < 100; i++) {
sum += i;
}
return sum;
}
}); System. out.println("主线程" + Thread.currentThread ().getName() + "在执行任务" ); try {
System. out.println("子线程运行结果" + future.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} System. out.println("所有任务执行完毕" ); }
}

Callable、Future和FutureTask使用说明的更多相关文章

  1. Java并发编程:ThreadPoolExecutor + Callable + Future(FutureTask) 探知线程的执行状况

    如题 (总结要点) 使用ThreadPoolExecutor来创建线程,使用Callable + Future 来执行并探知线程执行情况: V get (long timeout, TimeUnit ...

  2. Java并发编程:Callable、Future和FutureTask

    作者:海子 出处:http://www.cnblogs.com/dolphin0520/ 本博客中未标明转载的文章归作者海子和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置 ...

  3. Java多线程21:多线程下的其他组件之CyclicBarrier、Callable、Future和FutureTask

    CyclicBarrier 接着讲多线程下的其他组件,第一个要讲的就是CyclicBarrier.CyclicBarrier从字面理解是指循环屏障,它可以协同多个线程,让多个线程在这个屏障前等待,直到 ...

  4. Callable、Future和FutureTask

    创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable接口.这2种方式都有一个缺陷就是:在执行完任务之后无法获取执行结果. 如果需要获取执行结果,就必须通过共享变量或者使用线 ...

  5. Callable与Future、FutureTask的学习 & ExecutorServer 与 CompletionService 学习 & Java异常处理-重要

    Callable是Java里面与Runnable经常放在一起说的接口. Callable是类似于Runnable的接口,实现Callable接口的类和实现Runnable的类都是可被其他线程执行的任务 ...

  6. Java并发:Callable、Future和FutureTask

    Java并发编程:Callable.Future和FutureTask 在前面的文章中我们讲述了创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable接口. 这2种方式都有一 ...

  7. Runnable、Callable、Future和FutureTask用法

    http://www.cnblogs.com/dolphin0520/p/3949310.html java 1.5以前创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable ...

  8. Callable、Future和FutureTask区别

    在前面的文章中我们讲述了创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable接口. 这2种方式都有一个缺陷就是:在执行完任务之后无法获取执行结果. 如果需要获取执行结果,就 ...

  9. Java并发编程:Callable、Future和FutureTask的实现

    启动线程执行任务,如果需要在任务执行完毕之后得到任务执行结果,可以使用从Java 1.5开始提供的Callable和Future 下面就分析一下Callable.Future以及FutureTask的 ...

随机推荐

  1. js模拟键盘按键事件

    var WshShell = new ActiveXObject('WScript.Shell') WshShell.SendKeys('{ }'); 说明:大括号内的是键盘上的按键如: 空格:{ } ...

  2. (原创)win7自带IIS7.5+php7.0.10安装教程(图)

    php在上周8月18日发布了PHP 7.0 (7.0.10)版本.详细下载页面http://windows.php.net/download/,根据自身电脑配置情况酌情下载版本.win7旗舰版,iis ...

  3. 在生成 Visual c + + 2005年或从 DLL 文件中使用 CString 派生的类的 Visual c + +.net 应用程序时,您可能会收到 LNK2019 错误消息

    http://support.microsoft.com/kb/309801

  4. algorithm@ lower_bound implementation(Binary Search)

    一道来自jhu algorithm的作业题: Given two sorted arrays A, B, give a linear time algorithm that finds two entr ...

  5. leptonica 学习笔记2——pixBackgroundNormSimple

    1 pixBackgroundNormSimple 函数功能:自适应背影标准化 位置:adampmap.c /*-------------------------------------------- ...

  6. VisualStudio2010中创建ASP.Net WebService

    相关资料:http://blog.csdn.net/yapingxin/article/details/7331375 具体操作:1.打开“Microsoft Visual Studio 2010”- ...

  7. js为表格添加行和列

    <table id="studentTable" align="center" border="1px;" cellpadding=& ...

  8. iOS学习之NSBundle介绍和使用

    iOS学习之NSBundle介绍和使用 http://blog.csdn.net/totogo2010/article/details/7672271 新建一个Single View Applicat ...

  9. Spring Framework 5.0.0.M3中文文档 翻译记录 Part I. Spring框架概览1-2.2

    Part I. Spring框架概览 The Spring Framework is a lightweight solution and a potential one-stop-shop for ...

  10. URAL 1019 - Line Painting

    跟前面某个题一样,都是区间染色问题,还是用我的老方法,区间离散化+二分区间端点+区间处理做的,时间跑的还挺短 坑爹的情况就是最左端是0,最右端是1e9,区间求的是开区间 #include <st ...