JAVA线程池之Callable返回结果
本文介绍如何向线程池提交任务,并获得任务的执行结果。然后模拟 线程池中的线程在执行任务的过程中抛出异常时,该如何处理。
一,执行具体任务的线程类
要想 获得 线程的执行结果,需实现Callable接口。FactorialCalculator 计算 number的阶乘,具体实现如下: 1 import java.util.concurrent.Callable; 2 import java.util.concurrent.TimeUnit; 3 4 /** 5 * Created by Administrator on 2017/9/26. 6 */ 7 public class FactorialCalculator implements Callable { 8 9 private Integer number; 10 11 public FactorialCalculator(Integer number) { 12 this.number = number; 13 } 14 public Integer call() throws Exception { 15 int result = 1; 16 17 if (number == 0 || number == 1) { 18 result = 1; 19 }else { 20 for (int i = 2; i < number; i++) { 21 result *= i; 22 TimeUnit.MICROSECONDS.sleep(200); 23 if (i == 5) { 24 throw new IllegalArgumentException("excepion happend");//计算5以上的阶乘都会抛出异常. 根据需要注释该if语句 25 } 26 } 27 } 28 System.out.printf("%s: %d ", Thread.currentThread().getName(), result); 29 return result; 30 } 31 }
上面23行--25行的if语句表明:如果number大于5,那么 if(i==5)成立,会抛出异常。即模拟 执行5 以上的阶乘时,会抛出异常。
二,提交任务的Main类
下面来看看,怎样向线程池提交任务,并获取任务的返回结果。我们一共向线程池中提交了10个任务,因此创建了一个ArrayList保存每个任务的执行结果。
第一行,首先创建一个线程池。第二行,创建List保存10个线程的执行结果 所要存入的地方,每个任务是计算阶乘,因此线程的返回结果是 Integer。而这个结果只要计算出来了,是放在Future里面。
第5-7行,随机生成一个10以内的整数,然后创建一个 FactorialCalculator对象,该对象就是待执行的任务,然后在第8行 通过线程池的submit方法提交。 1 ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool(); 2 List> resultList = new ArrayList>(); 3 Random random = new Random(); 4 for (int i = 0; i < 10; i ++) { 5 int rand = random.nextInt(10); 6 7 FactorialCalculator factorialCalculator = new FactorialCalculator(rand); 8 Future res = executor.submit(factorialCalculator);//异步提交, non blocking. 9 resultList.add(res); 10 }
需要注意的是: submit方法是个非阻塞方法 ,参考这篇文章。提交了任务后,由线程池里面的线程负责执行该任务,执行完成后得到的结果最终会保存在 Future里面 ,正如第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循环中,检查任务的状态---是否执行完成。 1 do { 2 // System.out.printf("number of completed tasks: %d ", executor.getCompletedTaskCount()); 3 for (int i = 0; i < resultList.size(); i++) { 4 Future result = resultList.get(i); 5 System.out.printf("Task %d : %s ", i, result.isDone()); 6 } 7 try { 8 TimeUnit.MILLISECONDS.sleep(50); 9 10 } catch (InterruptedException e) { 11 e.printStackTrace(); 12 } 13 } while (executor.getCompletedTaskCount() < resultList.size());
第3-6行for循环,从ArrayList中取出 每个 Future,查看它的状态 isDone() ,即:是否执行完毕。
第13行,while结束条件:当所有的线程的执行完毕时,就退出do循环。注意:当线程在执行过程中抛出异常了,也表示线程执行完毕。
获取线程的执行结果 1 System.out.println("Results as folloers:"); 2 for (int i = 0; i < resultList.size(); i++) { 3 Future result = resultList.get(i); 4 Integer number = null; 5 6 try { 7 number = result.get();// blocking method 8 } catch (InterruptedException e) { 9 e.printStackTrace(); 10 } catch (ExecutionException e) { 11 e.printStackTrace(); 12 } 13 System.out.printf("task: %d, result %d: ", i, number); 14 }
第3行取出每个存储结果的地方:Future,第7行 从Future中获得任务的执行结果。 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> resultList = new ArrayList>(); Random random = new Random(); for (int i = 0; i < 10; i ++) { int rand = random.nextInt(10); FactorialCalculator factorialCalculator = new FactorialCalculator(rand); Future 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 ", executor.getCompletedTaskCount()); for (int i = 0; i < resultList.size(); i++) { Future result = resultList.get(i); System.out.printf("Task %d : %s ", 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 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: ", i, number); } executor.shutdown(); } }
在上面 number = result.get(); 语句中,我们 catch了两个异常,一个是InterruptedException,另一个是ExecutionException。因为我们是在main线程内获得任务的执行结果,main线程执行 result.get()会阻塞,如果在阻塞的过程中被(其他线程)中断,则抛出InterruptedException。
若在任务的执行过程中抛出了异常(比如IllegalArgumentException),那么main线程在这里就会catch到ExecutionException。此时,就可以对抛出异常的任务"进行处理"。此外,线程池中执行该任务的线程,并不会因为 该任务在执行过程中抛出了异常而受到影响,该线程 可以继续 接收并运行 下一次提交给它的任务。
图中 task1、task2、task4 返回的结果为空,表明它们抛出了IllegalArgumentException异常。
最后给大家分享Spring系列的学习笔记和面试题,包含spring面试题、spring cloud面试题、spring boot面试题、spring教程笔记、spring boot教程笔记、最新阿里巴巴开发手册(63页PDF总结)、2022年Java面试手册。一共整理了1184页PDF文档。私信博主(777)领取,祝大家更上一层楼!!!
原文作者:大熊猫同学
原文出处:http://www.cnblogs.com/hapjin/p/7599189.html
iPhone15Pro爆料压感震动替代物理按键在iPhone7系列上,苹果将Home键设计为不可按压设计,识别到手指用力按压后,会通过震动反馈来模拟机械按压的感觉,根据爆料,iPhone15Pro将针对音量键电源键也采用同样设
前三季度我国工业企业效益延续恢复态势新变化透露何种新趋势?国家统计局数据显示,19月,全国规模以上工业企业营业收入同比增长8。2,两类行业利润实现了今年的首次由降转增。前三季度我国工业企业效益延续恢复态势19月份,全国规模以上工业企业实现
日产汽车探讨向雷诺EV新公司出资15据共同社报道,日产汽车正探讨向法国汽车巨头雷诺力争成立的纯电动汽车(EV)新公司最多出资15。报道称,日产和三菱雷诺三家公司的企业联盟当天在线召开了会议,据分析,可能围绕向新公司出
锂矿价格大涨480,新能源汽车怎么玩?特斯拉全系降价自2021年价格疯涨以来,锂矿便被大家调侃为疯狂的石头,最近它又作妖了!10月24日,澳大利亚锂矿巨头Pilbara矿业发布公告称公司已完成新一批5000吨锂精矿的出售,成交价为7
vivoX90新机疑似入网,支持120W快充,或配备1英寸主摄前不久,vivo召开了一场影像战略发布会,介绍了新一代自研影像芯片以及创新的影像技术,同时也为vivoX90系列开始预热,宣布下一代影像旗舰产品将搭载更大的传感器,并且将于蔡司联合
放纵的思绪记得之前同事说过,大都市的生活往往是午夜过后才刚刚开始,此时刚好是凌晨12点整,这边的城市却己开始沉寂。甄点小酒,调节那略带忧感的心情,放着丝丝轻音乐,一首又一首的歌曲,才发现越听
光雾山红叶已达颜值巅峰,要来的赶紧,要不然等下一年景区介绍光雾山景区一共有四个,分别为小巫峡(看溶洞)大坝景区(看红叶以以及日出云雾)十八月潭(看红叶和十八个潭)桃园景区(看三生三世,不过十一月份不是看桃花的季节)。推荐的是大坝景
iPhone15系列再次被确认充电口配置运存基本都清晰了时代一直都在快速的发展,对于手机厂商来说不能做到一直都没有特别大的改变,如果一直都没有改变的话,那么消费者可能很难进行持续的进行买账。所以现在的市场竞争非常的残酷,各大厂商都在开发
iPhone14Pro难阻苹果下落iPhone新机又一次立功,扭转了苹果正在下滑的净利润。北京时间10月28日凌晨,苹果公司发布2022财年第四财季(即2022年度第三季度)财报。当季财报显示,苹果第三季度总营收9
黄斌汉新能源大风口!欧盟从2035年起禁产燃油车欧盟达成历史性协议,将从2035年起禁止生产新的燃油车。此举旨在加快电气化转型,并应对气候变化。欧盟委员会欧洲议会以及欧盟成员国的谈判代表一致同意,汽车制造商必须在2035年之前实
欧洲央行决定加息75个基点新华社法兰克福10月27日电(记者何丽丽)欧洲中央银行27日召开货币政策会议,决定将欧元区三大关键利率均上调75个基点。这是欧洲央行今年第三次加息。按照欧洲央行当天公布的货币政策决