JAVA 线程池之Callable返回结果
本文介绍如何向线程池提交任务,并获得任务的执行结果。然后模拟 线程池中的线程在执行任务的过程中抛出异常时,该如何处理。
一,执行具体任务的线程类
要想 获得 线程的执行结果,需实现Callable接口。FactorialCalculator 计算 number的阶乘,具体实现如下:
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit; /**
* Created by Administrator on 2017/9/26.
*/
public class FactorialCalculator implements Callable<Integer> { private Integer number; public FactorialCalculator(Integer number) {
this.number = number;
}
public Integer call() throws Exception {
int result = 1; if (number == 0 || number == 1) {
result = 1;
}else {
for (int i = 2; i < number; i++) {
result *= i;
TimeUnit.MICROSECONDS.sleep(200);
if (i == 5) {
throw new IllegalArgumentException("excepion happend");//计算5以上的阶乘都会抛出异常. 根据需要注释该if语句
}
}
}
System.out.printf("%s: %d\n", Thread.currentThread().getName(), result);
return result;
}
}
上面23行--25行的if语句表明:如果number大于5,那么 if(i==5)成立,会抛出异常。即模拟 执行5 以上的阶乘时,会抛出异常。
二,提交任务的Main类
下面来看看,怎样向线程池提交任务,并获取任务的返回结果。我们一共向线程池中提交了10个任务,因此创建了一个ArrayList保存每个任务的执行结果。
第一行,首先创建一个线程池。第二行,创建List保存10个线程的执行结果 所要存入的地方,每个任务是计算阶乘,因此线程的返回结果是 Integer。而这个结果只要计算出来了,是放在Future<Integer>里面。
第5-7行,随机生成一个10以内的整数,然后创建一个 FactorialCalculator对象,该对象就是待执行的任务,然后在第8行 通过线程池的submit方法提交。
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
List<Future<Integer>> resultList = new ArrayList<Future<Integer>>();
Random random = new Random();
for (int i = 0; i < 10; i ++) {
int rand = random.nextInt(10); FactorialCalculator factorialCalculator = new FactorialCalculator(rand);
Future<Integer> res = executor.submit(factorialCalculator);//异步提交, non blocking.
resultList.add(res);
}
需要注意的是:submit方法是个非阻塞方法,参考这篇文章。提交了任务后,由线程池里面的线程负责执行该任务,执行完成后得到的结果最终会保存在 Future<Integer>里面,正如第8行所示。
As soon as we invoke the submit() method of ExecutorService the Callable are handed over to ExecutorService to execute.
Here one thing we have to note, the submit() is not blocking.
So, all of our Callables will be submitted right away to the ExecutorService, and ExecutorService will decide when to execute which callable.
For each Callable we get a Future object to get the result later.
接下来,我们在do循环中,检查任务的状态---是否执行完成。
do {
// System.out.printf("number of completed tasks: %d\n", executor.getCompletedTaskCount());
for (int i = 0; i < resultList.size(); i++) {
Future<Integer> result = resultList.get(i);
System.out.printf("Task %d : %s \n", i, result.isDone());
}
try {
TimeUnit.MILLISECONDS.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (executor.getCompletedTaskCount() < resultList.size());
第3-6行for循环,从ArrayList中取出 每个 Future<Integer>,查看它的状态 isDone() ,即:是否执行完毕。
第13行,while结束条件:当所有的线程的执行完毕时,就退出do循环。注意:当线程在执行过程中抛出异常了,也表示线程执行完毕。
获取线程的执行结果
System.out.println("Results as folloers:");
for (int i = 0; i < resultList.size(); i++) {
Future<Integer> result = resultList.get(i);
Integer number = null;
try {
number = result.get();// blocking method
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
System.out.printf("task: %d, result %d:\n", i, number);
}
第3行取出每个存储结果的地方:Future<Integer>,第7行 从Future<Integer>中获得任务的执行结果。Future.get方法是一个阻塞方法。但前面的do-while循环里面,我们已经检查了任务的执行状态是否完成,因此这里能够很快地取出任务的执行结果。
We are invoking the get() method of Future to get the result. Here we have to remember that, the get() is a blocking method.
任务在执行过程中,若抛出异常,下面语句在获取执行结果时会直接跳到catch(ExecutionException e)中。
number = result.get()

但它不影响Main类线程---主线程的执行。

整个Main类代码如下:
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.*; /**
* Created by Administrator on 2017/9/26.
*/
public class Main { public static void main(String[] args) {
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
List<Future<Integer>> resultList = new ArrayList<Future<Integer>>();
Random random = new Random();
for (int i = 0; i < 10; i ++) {
int rand = random.nextInt(10); FactorialCalculator factorialCalculator = new FactorialCalculator(rand);
Future<Integer> res = executor.submit(factorialCalculator);//异步提交, non blocking.
resultList.add(res);
} // in loop check out the result is finished
do {
// System.out.printf("number of completed tasks: %d\n", executor.getCompletedTaskCount());
for (int i = 0; i < resultList.size(); i++) {
Future<Integer> result = resultList.get(i);
System.out.printf("Task %d : %s \n", i, result.isDone());
}
try {
TimeUnit.MILLISECONDS.sleep(50); } catch (InterruptedException e) {
e.printStackTrace();
}
} while (executor.getCompletedTaskCount() < resultList.size()); System.out.println("Results as folloers:");
for (int i = 0; i < resultList.size(); i++) {
Future<Integer> result = resultList.get(i);
Integer number = null; try {
number = result.get();// blocking method
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
System.out.printf("task: %d, result %d:\n", i, number);
}
executor.shutdown();
}
}
在上面 number = result.get(); 语句中,我们 catch了两个异常,一个是InterruptedException,另一个是ExecutionException。因为我们是在main线程内获得任务的执行结果,main线程执行 result.get()会阻塞,如果在阻塞的过程中被(其他线程)中断,则抛出InterruptedException。
若在任务的执行过程中抛出了异常(比如IllegalArgumentException),那么main线程在这里就会catch到ExecutionException。此时,就可以对抛出异常的任务“进行处理”。此外,线程池中执行该任务的线程,并不会因为 该任务在执行过程中抛出了异常而受到影响,该线程 可以继续 接收并运行 下一次提交给它的任务。
图中 task1、task2、task4 返回的结果为空,表明它们抛出了IllegalArgumentException异常。
而要想对异常进行处理,可参考:Java线程池异常处理最佳实践和 这篇文章。
参考资料
《Java 7 Concurrency Cookbook》 chapter 4
原文:http://www.cnblogs.com/hapjin/p/7599189.html
JAVA 线程池之Callable返回结果的更多相关文章
- Java线程池(Callable+Future模式)
转: Java线程池(Callable+Future模式) Java线程池(Callable+Future模式) Java通过Executors提供四种线程池 1)newCachedThreadPoo ...
- Java线程池 / Executor / Callable / Future
为什么需要线程池? 每次都要new一个thread,开销大,性能差:不能统一管理:功能少(没有定时执行.中断等). 使用线程池的好处是,可重用,可管理. Executor 4种线程 ...
- Java线程池学习
Java线程池学习 Executor框架简介 在Java 5之后,并发编程引入了一堆新的启动.调度和管理线程的API.Executor框架便是Java 5中引入的,其内部使用了线程池机制,它在java ...
- java线程池的使用与详解
java线程池的使用与详解 [转载]本文转载自两篇博文: 1.Java并发编程:线程池的使用:http://www.cnblogs.com/dolphin0520/p/3932921.html ...
- java线程池分析和应用
比较 在前面的一些文章里,我们已经讨论了手工创建和管理线程.在实际应用中我们有的时候也会经常听到线程池这个概念.在这里,我们可以先针对手工创建管理线程和通过线程池来管理做一个比较.通常,我们如果手工创 ...
- java 线程池 并行 执行
https://github.com/donaldlee2008/JerryMultiThread/blob/master/src/com/jerry/threadpool/ThreadPoolTes ...
- Java线程池使用和分析(一)
线程池是可以控制线程创建.释放,并通过某种策略尝试复用线程去执行任务的一种管理框架,从而实现线程资源与任务之间的一种平衡. 以下分析基于 JDK1.7 以下是本文的目录大纲: 一.线程池架构 二.Th ...
- JAVA线程池应用的DEMO
在做很多高并发应用的时候,单线程的瓶颈已经满足不了我们的需求,此时使用多线程来提高处理速度已经是比较常规的方案了.在使用多线程的时候,我们可以使用线程池来管理我们的线程,至于使用线程池的优点就不多说了 ...
- Java线程池详解
一.线程池初探 所谓线程池,就是将多个线程放在一个池子里面(所谓池化技术),然后需要线程的时候不是创建一个线程,而是从线程池里面获取一个可用的线程,然后执行我们的任务.线程池的关键在于它为我们管理了多 ...
随机推荐
- CF1139D Steps to One(DP,莫比乌斯反演,质因数分解)
stm这是div2的D题……我要对不住我这个紫名了…… 题目链接:CF原网 洛谷 题目大意:有个一开始为空的序列.每次操作会往序列最后加一个 $1$ 到 $m$ 的随机整数.当整个序列的 $\gcd ...
- poj 3279 Fliptile(二进制搜索)
Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He ha ...
- Shiro中的Rememberme后出现浏览器500错误
问题详述:在Shiro中添加Remember me功能后,只要勾选Remember me选项为true的时候,浏览器就会跳转到一个不可达页面,并且在Chrome中显示HTTP 500错误. 问题追踪: ...
- maven插件运行过程中自动执行sql文件
配置pom.propertis即可 <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId> ...
- highstock+websocket实现动态展现
效果:从后台获取回测数据,在前端动态展现,和聚宽实现的回测效果相仿 大体思路:先传一个[[int,0],[int,0],[int,0],[int,0],[int,0],...]格式的死数据到前端渲染x ...
- An Introduction to OAuth 2
PostedJuly 21, 2014 1.1mviews SECURITY API CONCEPTUAL Mitchell Anicas Introduction OAuth 2 is an aut ...
- Ubuntu中VisualBox无法识别USB设备
解决方法 安装Oracle VM VirtualBox Extension Pack( https://www.virtualbox.org/wiki/Downloads ) 执行sudo /usr/ ...
- 洛谷P1224 向量内积
什么毒瘤...... 题意:给定n个d维向量,定义向量a和b的内积为 求是否存在两个向量使得它们的内积为k的倍数,并给出任一种方案.k <= 3. 解:很容易想到一个暴力是n2d的.显然我们不能 ...
- PHP的SQL语句优化
(转)仅供自己学习,特此转发 普遍遇到的慢SQL有以下三种: 1.未走索引 2.where条件里包含子查询,多表联查 3.查询大量数据 解决 一.索引:SQL中的高速公路 但凡优化SQL,首先要看的就 ...
- Redis:默认配置文件redis.conf详解
转: Redis:默认配置文件redis.conf详解 # Redis配置文件样例 # Note on units: when memory size is needed, it is possibl ...