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线程池详解
一.线程池初探 所谓线程池,就是将多个线程放在一个池子里面(所谓池化技术),然后需要线程的时候不是创建一个线程,而是从线程池里面获取一个可用的线程,然后执行我们的任务.线程池的关键在于它为我们管理了多 ...
随机推荐
- CodeForces 97 E. Leaders(点双连通分量 + 倍增)
题意 给你一个有 \(n\) 个点 \(m\) 条边的无向图,有 \(q\) 次询问,每次询问两个点 \(u, v\) 之间是否存在长度为奇数的简单路径. \(1 \le n, m, q \le 10 ...
- Python将是人工智能时代的最佳编程语言
Python将是人工智能时代的最佳编程语言 移动互联网取代PC互联网领跑在互联网时代的最前沿,Android和iOS一度成为移动互联网应用平台的两大霸主,成为移动开发者首选的两门技术,HTML5以其跨 ...
- Codeforces | CF1010C 【Border】
这道题大致题意是给定\(n\)个十进制整数和一个进制数\(k\),可以用无数多个给定的十进制整数,问这些十进制整数的和在模k意义下有多少种不同的结果(\(k\)进制下整数的最后一位就是这个数模\(k\ ...
- 用c#查询各快递物流信息
前面介绍过<用快递100接口查询各快递物流信息>,里面用的是API接口,但有些快递不支持这种方式,本文介绍的HtmlAPI支持所有快递公司. 支持包括EMS.邮政.顺丰和申通在内的所有公司 ...
- pip 安装第三方包提示Unknown or unsupported command 'install'
Unknown or unsupported command 'install' Unknown or unsupported command 'show' Unknown or unsupporte ...
- scrapy 选择器
1.lxml(转自简书) from lxml import etree2 import requests3 45 url = " "6 html = requests.get(ur ...
- 20165223 week4测试补交与总结
JDB调试程序 调试代码 public class SumofRecur1{ public static void main(String[] args) { int i = 0; for(Strin ...
- Spring Boot 与 OAuth2 官方最详细教程
https://mp.weixin.qq.com/s?__biz=MzU0MDEwMjgwNA==&mid=2247484357&idx=1&sn=73e501de8591e6 ...
- NPOI的一些基本操作
1,创建一个Excel //创建一个工作簿 XSSFWorkbook workbook = new XSSFWorkbook(); //创建一个页 ISheet sheet = workbook.Cr ...
- [luogu3391][文艺平衡树]
题目链接 思路 splay区间操作的裸题. 假如要对l-r这段区间操作,那么就先把l-1伸展到根节点,然后把r +1伸展为根的儿子.这样r + 1的左儿子就是要操作的区间了.只要在上面打上标记,以后每 ...