一、采用take()方法时发生异常

示例代码:

情况一:异常比另一个正确任务,较晚出现,正确任务的结果会打印出

import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; public class MyCompletionServiceException { public static void main(String[] args) {
// TODO 自动生成的方法存根
ExecutorService executor=Executors.newCachedThreadPool();
CompletionService<String> comservice=new ExecutorCompletionService<String>(executor);
MyException_one callone=new MyException_one();
MyException_two calltwo=new MyException_two();
comservice.submit(callone);
comservice.submit(calltwo); try {
// Thread.sleep(3000);
System.out.println(comservice.take().get());
System.out.println(comservice.take().get());
// System.out.println(comservice.poll().get());
// System.out.println(comservice.poll().get());
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (ExecutionException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
} }
class MyException_one implements Callable<String>{ @Override
public String call() throws Exception {
// TODO 自动生成的方法存根
Thread.sleep(2000);
if(true){
throw new Exception("抛出异常");
}
return "one";
} }
class MyException_two implements Callable<String>{ @Override
public String call() throws Exception {
// TODO 自动生成的方法存根
Thread.sleep(1000);
return "two";
} }

运行结果:

import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; public class MyCompletionServiceException { public static void main(String[] args) {
// TODO 自动生成的方法存根
ExecutorService executor=Executors.newCachedThreadPool();
CompletionService<String> comservice=new ExecutorCompletionService<String>(executor);
MyException_one callone=new MyException_one();
MyException_two calltwo=new MyException_two();
comservice.submit(callone);
comservice.submit(calltwo); try {
// Thread.sleep(3000);
System.out.println(comservice.take().get());
System.out.println(comservice.take().get());
// System.out.println(comservice.poll().get());
// System.out.println(comservice.poll().get());
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (ExecutionException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
} }
class MyException_one implements Callable<String>{ @Override
public String call() throws Exception {
// TODO 自动生成的方法存根
Thread.sleep(2000);
if(true){
throw new Exception("抛出异常");
}
return "one";
} }
class MyException_two implements Callable<String>{ @Override
public String call() throws Exception {
// TODO 自动生成的方法存根
Thread.sleep(1000);
return "two";
} }

  

当只采用take()方法,而不使用get()方法时不出现异常,改为:

System.out.println(comservice.take().get());
System.out.println(comservice.take());

  运行结果:

two
java.util.concurrent.FutureTask@1f96302

  情况二:

异常比另一个正确任务较早出现,这时不会打印出另一个正确任务的结果

示例代码(贴出有修改的地方):

class MyException_one implements Callable<String>{

	@Override
public String call() throws Exception {
// TODO 自动生成的方法存根
Thread.sleep(1000);
if(true){
throw new Exception("抛出异常");
}
return "one";
} }
class MyException_two implements Callable<String>{ @Override
public String call() throws Exception {
// TODO 自动生成的方法存根
Thread.sleep(3000);
return "two";
} }

  运行结果:

java.util.concurrent.ExecutionException: java.lang.Exception: 抛出异常
at java.util.concurrent.FutureTask.report(Unknown Source)
at java.util.concurrent.FutureTask.get(Unknown Source)
at mycompletionservice.MyCompletionServiceException.main(MyCompletionServiceException.java:23)
Caused by: java.lang.Exception: 抛出异常
at mycompletionservice.MyException_one.call(MyCompletionServiceException.java:44)
at mycompletionservice.MyException_one.call(MyCompletionServiceException.java:1)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

  

二、采用poll()方法时发生异常

情况一:异常比另一个正确任务,较晚出现,正确任务的结果会打印出

示例代码:

import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; public class MyCompletionServiceException { public static void main(String[] args) {
// TODO 自动生成的方法存根
ExecutorService executor=Executors.newCachedThreadPool();
CompletionService<String> comservice=new ExecutorCompletionService<String>(executor);
MyException_one callone=new MyException_one();
MyException_two calltwo=new MyException_two();
comservice.submit(calltwo);
comservice.submit(callone); try {
Thread.sleep(3000);
// System.out.println(comservice.take().get());
// System.out.println(comservice.take().get());
System.out.println(comservice.poll().get());
System.out.println(comservice.poll().get());
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (ExecutionException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
} }
class MyException_one implements Callable<String>{ @Override
public String call() throws Exception {
// TODO 自动生成的方法存根
Thread.sleep(2000);
if(true){
throw new Exception("抛出异常");
}
return "one";
} }
class MyException_two implements Callable<String>{ @Override
public String call() throws Exception {
// TODO 自动生成的方法存根
Thread.sleep(1000);
return "two";
} }

  运行结果:

two
java.util.concurrent.ExecutionException: java.lang.Exception: 抛出异常
at java.util.concurrent.FutureTask.report(Unknown Source)
at java.util.concurrent.FutureTask.get(Unknown Source)
at mycompletionservice.MyCompletionServiceException.main(MyCompletionServiceException.java:26)
Caused by: java.lang.Exception: 抛出异常
at mycompletionservice.MyException_one.call(MyCompletionServiceException.java:44)
at mycompletionservice.MyException_one.call(MyCompletionServiceException.java:1)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

  同样不是get()时不会出现异常

情况二:

异常比另一个正确任务较早出现,这时不会打印出另一个正确任务的结果

示例代码:

class MyException_one implements Callable<String>{

	@Override
public String call() throws Exception {
// TODO 自动生成的方法存根
Thread.sleep(1000);
if(true){
throw new Exception("抛出异常");
}
return "one";
} }
class MyException_two implements Callable<String>{ @Override
public String call() throws Exception {
// TODO 自动生成的方法存根
Thread.sleep(2000);
return "two";
} }

  运行结果:

class MyException_one implements Callable<String>{

	@Override
public String call() throws Exception {
// TODO 自动生成的方法存根
Thread.sleep(1000);
if(true){
throw new Exception("抛出异常");
}
return "one";
} }
class MyException_two implements Callable<String>{ @Override
public String call() throws Exception {
// TODO 自动生成的方法存根
Thread.sleep(2000);
return "two";
} }

  

import java.util.concurrent.Callable;import java.util.concurrent.CompletionService;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorCompletionService;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;
public class MyCompletionServiceException {
public static void main(String[] args) {// TODO 自动生成的方法存根ExecutorService executor=Executors.newCachedThreadPool();CompletionService<String> comservice=new ExecutorCompletionService<String>(executor);MyException_one callone=new MyException_one();MyException_two calltwo=new MyException_two();comservice.submit(calltwo);comservice.submit(callone);try {Thread.sleep(3000);//System.out.println(comservice.take().get());//System.out.println(comservice.take().get());System.out.println(comservice.poll().get());System.out.println(comservice.poll().get());} catch (InterruptedException e) {// TODO 自动生成的 catch 块e.printStackTrace();} catch (ExecutionException e) {// TODO 自动生成的 catch 块e.printStackTrace();}}
}class MyException_one implements Callable<String>{
@Overridepublic String call() throws Exception {// TODO 自动生成的方法存根Thread.sleep(2000);if(true){throw new Exception("抛出异常");}return "one";}}class MyException_two implements Callable<String>{
@Overridepublic String call() throws Exception {// TODO 自动生成的方法存根Thread.sleep(1000);return "two";}}

CompletionService的异常处理的更多相关文章

  1. Callable与Future、FutureTask的学习 & ExecutorServer 与 CompletionService 学习 & Java异常处理-重要

    Callable是Java里面与Runnable经常放在一起说的接口. Callable是类似于Runnable的接口,实现Callable接口的类和实现Runnable的类都是可被其他线程执行的任务 ...

  2. 012-Future、FutureTask、CompletionService 、CompletableFuture

    一.概述 创建线程的两种方式,一种是直接继承Thread,另外一种就是实现Runnable接口.这两种方式都有一个缺陷就是:在执行完任务之后无法获取执行结果.如果需要获取执行结果,就必须通过共享变量或 ...

  3. 异步并发利器:实际项目中使用CompletionService提升系统性能的一次实践

    场景 随着互联网应用的深入,很多传统行业也都需要接入到互联网.我们公司也是这样,保险核心需要和很多保险中介对接,比如阿里.京东等等.这些公司对于接口服务的性能有些比较高的要求,传统的核心无法满足要求, ...

  4. 实际项目中使用CompletionService提升系统性能的一次实践

    随着互联网应用的深入,很多传统行业也都需要接入到互联网.我们公司也是这样,保险核心需要和很多保险中介对接,比如阿里.京东等等.这些公司对于接口服务的性能有些比较高的要求,传统的核心无法满足要求,所以信 ...

  5. 关于.NET异常处理的思考

    年关将至,对于大部分程序员来说,马上就可以闲下来一段时间了,然而在这个闲暇的时间里,唯有争论哪门语言更好可以消磨时光,估计最近会有很多关于java与.net的博文出现,我表示要作为一个吃瓜群众,静静的 ...

  6. 基于spring注解AOP的异常处理

    一.前言 项目刚刚开发的时候,并没有做好充足的准备.开发到一定程度的时候才会想到还有一些问题没有解决.就比如今天我要说的一个问题:异常的处理.写程序的时候一般都会通过try...catch...fin ...

  7. 异常处理汇总 ~ 修正果带着你的Net飞奔吧!

    经验库开源地址:https://github.com/dunitian/LoTDotNet 异常处理汇总-服 务 器 http://www.cnblogs.com/dunitian/p/4522983 ...

  8. JavaScript var关键字、变量的状态、异常处理、命名规范等介绍

    本篇主要介绍var关键字.变量的undefined和null状态.异常处理.命名规范. 目录 1. var 关键字:介绍var关键字的使用. 2. 变量的状态:介绍变量的未定义.已定义未赋值.已定义已 ...

  9. IL异常处理

    异常处理在程序中也算是比较重要的一部分了,IL异常处理在C#里面实现会用到一些新的方法 1.BeginExceptionBlock:异常块代码开始,相当于try,但是感觉又不太像 2.EndExcep ...

随机推荐

  1. day--40 mysql-视图,触发器,存储过程,函数总结

    视图,触发器,存储过程,函数总结 一:视图 01:介绍 视图是一个虚拟表(非真实存在),是跑到内存中的表,真实表是硬盘上的表,怎么就得到了虚拟表,就是你查询的结果,只不过之 前我们查询出来的虚拟表,从 ...

  2. POJ3635 Full Tank? 优先队列BFS or 分层图最短路 or DP?

    然而我也不知道这是啥啊...反正差不多...哪位大佬给区分一下QWQ.. 好的,我把堆的<写反了..又调了一个小时..你能不能稳一点.... 记录状态:所在位置u,油量c,花费w 扩展状态: 1 ...

  3. mock static方法

    <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mock ...

  4. 选择IM云服务供应商

    选择IM云服务供应商,其实最重要是有三个因素:费用.技术稳定性.以及后续运维服务. 对于不少创业公司来讲,可能需要找到成本和稳定性的最佳平衡点.目前国内不少IM云服务产品都推出了免费服务项目或者一定期 ...

  5. PIE SDK专题制图下屏幕坐标转地图坐标

    1.    功能简介 PIESDK提供了专题制图下鼠标屏幕坐标转地图坐标功能. 2. 功能实现说明 2.1屏幕坐标转地图坐标 此功能用到了IPageLayout.ToMapPoint()方法,它的主要 ...

  6. MAC 下 STF 的环境搭建和运行

    STF --WEB 端批量移动设备管理控制工具 安装各种包 (首先安装Macport,因为后面需要用到port:http://www.ccvita.com/434.html) linux的基本包安装, ...

  7. AWS and OpenStack

    AWS OpenStack EC2 Nova EBS Cinder EFS Manila S3 Swift Storage Gateway 本地上云 ClondFront 内容发布服务 VPC Neu ...

  8. 读取日志文件,搜索关键字,打印关键字前5行。yield、deque实例

    from collections import deque def search(lines, pattern, history=5): previous_lines = deque(maxlen=h ...

  9. underscore javascript工具库支持seajs模块化

    underscore是一个很有用的js工具库,但是好像默认不支持seajs模块化 新建一个文件例如叫做xx.js 谈后,键入 define(function(require,exports,modul ...

  10. pat1003. Emergency (25)

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...