本文介绍如何向线程池提交任务,并获得任务的执行结果。然后模拟 线程池中的线程在执行任务的过程中抛出异常时,该如何处理。

一,执行具体任务的线程类

要想 获得 线程的执行结果,需实现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返回结果的更多相关文章

  1. Java线程池(Callable+Future模式)

    转: Java线程池(Callable+Future模式) Java线程池(Callable+Future模式) Java通过Executors提供四种线程池 1)newCachedThreadPoo ...

  2. Java线程池 / Executor / Callable / Future

    为什么需要线程池?   每次都要new一个thread,开销大,性能差:不能统一管理:功能少(没有定时执行.中断等).   使用线程池的好处是,可重用,可管理.   Executor     4种线程 ...

  3. Java线程池学习

    Java线程池学习 Executor框架简介 在Java 5之后,并发编程引入了一堆新的启动.调度和管理线程的API.Executor框架便是Java 5中引入的,其内部使用了线程池机制,它在java ...

  4. java线程池的使用与详解

    java线程池的使用与详解 [转载]本文转载自两篇博文:  1.Java并发编程:线程池的使用:http://www.cnblogs.com/dolphin0520/p/3932921.html   ...

  5. java线程池分析和应用

    比较 在前面的一些文章里,我们已经讨论了手工创建和管理线程.在实际应用中我们有的时候也会经常听到线程池这个概念.在这里,我们可以先针对手工创建管理线程和通过线程池来管理做一个比较.通常,我们如果手工创 ...

  6. java 线程池 并行 执行

    https://github.com/donaldlee2008/JerryMultiThread/blob/master/src/com/jerry/threadpool/ThreadPoolTes ...

  7. Java线程池使用和分析(一)

    线程池是可以控制线程创建.释放,并通过某种策略尝试复用线程去执行任务的一种管理框架,从而实现线程资源与任务之间的一种平衡. 以下分析基于 JDK1.7 以下是本文的目录大纲: 一.线程池架构 二.Th ...

  8. JAVA线程池应用的DEMO

    在做很多高并发应用的时候,单线程的瓶颈已经满足不了我们的需求,此时使用多线程来提高处理速度已经是比较常规的方案了.在使用多线程的时候,我们可以使用线程池来管理我们的线程,至于使用线程池的优点就不多说了 ...

  9. Java线程池详解

    一.线程池初探 所谓线程池,就是将多个线程放在一个池子里面(所谓池化技术),然后需要线程的时候不是创建一个线程,而是从线程池里面获取一个可用的线程,然后执行我们的任务.线程池的关键在于它为我们管理了多 ...

随机推荐

  1. 【Luogu3602】Koishi Loves Segments(贪心)

    [Luogu3602]Koishi Loves Segments(贪心) 题面 洛谷 题解 离散区间之后把所有的线段挂在左端点上,从左往右扫一遍. 对于当前点的限制如果不满足显然会删掉右端点最靠右的那 ...

  2. iptables(1)

    iptables配置文件:/etc/sysconfig/iptables 确认开启路由转发功能方法1:/sbin/sysctl -w net.ipv4.ip_forward=1方法2:echo 1 & ...

  3. 构建DHCP服务

    --------------------DHCP 配置-------------------# yum install dhcp -y# vim /etc/dhcp/dhcpd.conf# cp /u ...

  4. 使用unittest单元测试框架对加法做单元测试

    import unittest from parameterized import parameterized def cacl(a, b): return a+b class MyCacl(unit ...

  5. luogu5020 [NOIp2018]货币系统 (完全背包)

    我那个新的货币系统,就是把原来的货币系统中能被其他数表示的数删掉 那我就算有多少数能被别的数表示,那肯定是要被比它小的表示 于是排个序做完全背包就好了 但是我太zz不会完全背包,然后写了个bitset ...

  6. 解决VS2012 服务器资源管理器中的表拖不到Linq to sql中

    找到C:\Program Files (x86)\Common Files\microsoft shared\Visual Database Tools\dsref80.dll 这个dll文件: 在其 ...

  7. Python三大web框架简单介绍

    Django 是重量级框架:它封装的的功能常丰富非常多所以它是重量级,Django的文档最完善.市场占有率最高.招聘职位最多.Django提供全套的解决方案(full-stack framework ...

  8. 踩过的坑—iphone手机H5样式兼容总结

    对一个前端开发者来说,最煎熬的莫过于"兼容"两个字了(说到这个词朋友们是不是身体一抖),哪怕对于工作多年的老油条来讲,也不是完全了解各种场景下的兼容性处理方法.在这里我就把我在工作 ...

  9. POJ 3522 Slim Span(极差最小生成树)

    Slim Span Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 9546   Accepted: 5076 Descrip ...

  10. (最长回文子串 线性DP) 51nod 1088 最长回文子串

    输入一个字符串Str,输出Str里最长回文子串的长度. 回文串:指aba.abba.cccbccc.aaaa这种左右对称的字符串. 串的子串:一个串的子串指此(字符)串中连续的一部分字符构成的子(字符 ...