CallableRunnable的区别如下:

I    Callable定义的方法是call,而Runnable定义的方法是run。

II   Callable的call方法可以有返回值,而Runnable的run方法不能有返回值。

III  Callable的call方法可抛出异常,而Runnable的run方法不能抛出异常。

Future 介绍

Future表示异步计算的结果,它提供了检查计算是否完成的方法,以等待计算的完成,并检索计算的结果。Future的cancel方法可以取消任务的执行,它有一布尔参数,参数为 true 表示立即中断任务的执行,参数为 false 表示允许正在运行的任务运行完成。Future的 get 方法等待计算完成,获取计算结果。

import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; public class CallableAndFuture {
public static void main(String[] args) {
ExecutorService threadPool = Executors.newCachedThreadPool();
CompletionService<Integer> cs = new ExecutorCompletionService<Integer>(threadPool);
for(int i = 1; i < 5; i++) {
final int taskID = i;
cs.submit(new Callable<Integer>() {
public Integer call() throws Exception {
return taskID;
}
});
} for(int i = 1; i < 5; i++) {
try {
System.out.println(cs.take().get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
}
其实也可以不使用CompletionService,可以先创建一个装Future类型的集合,用Executor提交的任务返回值添加到集合中,最后遍历集合取出数据
import java.util.ArrayList;
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 CallableAndFuture {
public static void main(String[] args) {
//用future实现
ExecutorService threadPool = Executors.newCachedThreadPool();
ArrayList<Future<Integer>> results = new ArrayList<Future<Integer>>();
for(int i = 1; i < 5; i++) {
final int taskID = i;
results.add(threadPool.submit(new Callable<Integer>() {
public Integer call() throws Exception {
return taskID;
}
}));
} for(int i = 1; i < 5; i++) {
try {
System.out.println(results.get(i-1).get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
}

CallableAndFuture的更多相关文章

  1. 并发包CallableAndFuture

    /** * * @描述: Callable Future * * 程序运行一个线程,线程运行结束后,我们可以获取另一个线程的结果 * * @作者: Wnj . * @创建时间: 2017年5月16日 ...

  2. java 多线程 day10 获取线程的返回值 CallableAndFuture

    import java.util.Random;import java.util.concurrent.*; /** * Created by chengtao on 17/12/4. * * 获取线 ...

  3. Java线程(七):Callable和Future

    转自:http://blog.csdn.net/ghsau/article/details/7451464 本篇说明的是Callable和Future,它俩很有意思的,一个产生结果,一个拿到结果. C ...

  4. java核心知识点学习----创建线程的第三种方式Callable和Future CompletionService

    前面已经指出通过实现Runnable时,Thread类的作用就是将run()方法包装成线程执行体,那么是否可以直接把任意方法都包装成线程执行体呢?Java目前不行,但其模仿者C#中是可以的. Call ...

  5. 【Java线程】Callable和Future

    Future模式 Future接口是Java线程Future模式的实现,可以来进行异步计算. Future模式可以这样来描述: 我有一个任务,提交给了Future,Future替我完成这个任务.期间我 ...

  6. Callable与Future的简单介绍

    Callable与Future的介绍 Callable与 Future 两功能是Java在后续版本中为了适应多并法才加入的,Callable是类似于Runnable的接口,实现Callable接口的类 ...

  7. 使用CompletionService结合ExecutorService批处理任务

    CompletionService用于提交一组Callable任务,其take方法返回已完成的一个Callable任务对应的Future对象. 如果你向Executor提交了一个批处理任务,并且希望在 ...

  8. 多线程08-Callable和Future

    1.简介 Callable是一个接口,与Runnable类似,包含一个必须实现的call方法,可以启动为让另一个线程来执行,执行Callable可以得到一个Future对象 该对象可以监听Callab ...

  9. java并发编程--Executor框架(一)

    摘要:        Eexecutor作为灵活且强大的异步执行框架,其支持多种不同类型的任务执行策略,提供了一种标准的方法将任务的提交过程和执行过程解耦开发,基于生产者-消费者模式,其提交任务的线程 ...

随机推荐

  1. Excel将秒转换成标准的时间格式HH:MM:SS

    Excel将秒转换成标准的时间格式HH:MM:SS 比如120秒,转换成00:02:00 Excel公式为: =TEXT(A1/86400,"[hh]:mm:ss") A1为秒数据 ...

  2. Java基础之集合框架——使用集合Vector<>挑选演员(TryVector)

    控制台程序. public class Person implements Comparable<Person> { // Constructor public Person(String ...

  3. 单链表的回文判断(O(n)时间复杂度和O(1)的空间复杂度)

    对于单链表来说,判断回文最简单的方法就是遍历链表,将链表中的元素复制到数组中,然后对数组进行判断是否是回文数组,但是这不符合O(1)的空间复杂度. 由于空间复杂度的要求,需要就地操作链表,不能开辟多余 ...

  4. iOS 延迟执行

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(<#delayInSeconds#> * NSEC_PER_SEC)), ...

  5. Swift游戏实战-跑酷熊猫 10 视差滚动背景

    原理 实现 勘误 “实现”的视频中有个错误,如下 背景移动时有个错误,看红色部分,近景归位时,第二张图片的下标是1 if arrBG[0].position.x + arrBG[0].frame.wi ...

  6. PostgreSQL Partitions

    why we need partitions The first and most demanding reason to use partitions in a database is to inc ...

  7. HTML部分标签和代码

    1.1.1 一般标签.格式控制标签 所谓HTML就是 Hyper Text Markup Language(超无文本标记语言) <html>   开始标签 <head> 网页上 ...

  8. new XMLHttpRequest()和页面关系

    1.  三个页面分别对应"自己“的异步对象(3个) <title></title> <script type="text/javascript&quo ...

  9. csuoj 1337: 搞笑版费马大定理

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1337 1337: 搞笑版费马大定理 Time Limit: 1 Sec  Memory Limit ...

  10. 输入和输出的总结(c语言)

    c语言中有多种的输入和输出方式,下面就简单总结一下: 一.输入的三种方式 (1)scanf scanf 函数可以在变量中使用,也可以在数组中使用,当然指针上也能用到,是一个很好的输入函数.scanf是 ...