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

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

要想 获得 线程的执行结果,需实现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. 部署KVM

    1.安装前准备1)服务器或者PC的CPU能支持VT技术2)虚拟机中安装KVM要勾选:处理器:虚拟化Intel VT-x/EPT或AMD-V/RVI(V)[root@localhost ~]# cat ...

  2. HR_Sherlock and Anagrams_TIMEOUT[UNDONE]

    2019年1月10日15:39:23 去掉了所有不必要的循环区间 还是超时 本地运行大概3s #!/bin/python3 import math import os import random im ...

  3. php 限制类的对象类型

    事实上,采用哪种处理参数类型的策略,取决于任何潜在bug的严重程度.通常PHP会根据语境自动转换大多数基本数据类型. 因此,你需要在检测类型.转换类型和依赖良好清晰的文档(无论决定用哪一种,都应该提供 ...

  4. ACM-ICPC 2015 BeiJing

    比赛连接:ACM-ICPC 2015 BeiJing 本次比赛只写了 A G     然后 I题随后补 A 有一个正方形土地,上面有若干块绿洲.让你以x0为界限划一条竖线,要求左边绿洲面积>=右 ...

  5. CodeFroces-- 514.div2.C-Sequence Transformation

    题目链接 :514.div2.C-Sequence Transformation #include<bits/stdc++.h> using namespace std; #define ...

  6. BZOJ 1370: [Baltic2003]Gang团伙(luogu 1892)(种类并查集)

    题面: bzoj题面有误,还是看luogu的吧 https://www.luogu.org/problemnew/show/P1892 题解: 种类并查集.. 因为有敌人的敌人是朋友这个条件,所以需要 ...

  7. 五大理由分析Springboot 2.0为什么选择HikariCP

    五大理由分析Springboot 2.0为什么选择HikariCP 2018-05-04 工匠小猪猪 占小狼的博客 本文非原创,是工匠小猪猪的技术世界搜集了一些HikariCP相关的资料整理给大家的介 ...

  8. list根据某个字段去重

    方法一:使用Set List<User> newList = new ArrayList<User>(); Set<String> set = new HashSe ...

  9. 用lemon测交互题

    题目类型:传统. 答案比较类型:逐行比较类型(忽略多余空格和制表符). 配置:交互. 编译器参数: -o %s %s.* ..\..\data\%s\judge.cpp -Wl,--stack= ju ...

  10. 用 Homebrew 带飞你的 Mac

    文章目录 资料 安装 基本用法 源镜像 Homebrew也称brew,macOS下基于命令行的最强大软件包管理工具,使用Ruby语言开发.类似于CentOS的yum或者Ubuntu的apt-get,b ...