一、采用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. 011 Android TabLayout+ViewPager实现顶部滑动效果(多个页面)

    1.TabLayout介绍 TabLayout提供了一个水平的布局用来展示Tabs,很多应用都有这样的设计,典型的有网易新闻,简书,知乎等.TabLayout就可以很好的完成这一职责,首先TabLay ...

  2. em 与 rem 区别.

    em 与自身  字体大小有关. rem 与 body 的字体大小有关..

  3. python学习,day1:循环判断基本语句的几个代码

    # coding=utf-8 # Author: RyAn Bi count = 0 '''while True : print('count:',count) count = count + 1 i ...

  4. R语言常用包汇总

    转载于:https://blog.csdn.net/sinat_26917383/article/details/50651464?locationNum=2&fps=1 一.一些函数包大汇总 ...

  5. json语法和使用

    一.JSON 概述: JavaScript Object Natation,是一种轻量级的数据交换技术规范. 二.使用流程: 在服务端将java对象转换为JSON,然后发送到浏览器,在浏览器上在讲JS ...

  6. P2056 [ZJOI2007]捉迷藏

    传送门 如果没有修改显然就直接点分治 有修改那就动态点分治 动态点分治就是在点分树上维护一些东西,查询时也在点分树上查 因为点分树深度是$log$的所以可以保证时间复杂度 此题我们需要在点分树上维护 ...

  7. poj2393tmp

    #include"iostream" #include"stdio.h" #include"algorithm" using namespa ...

  8. mysql 命令笔记

    添加密码 mysqladmin -uroot -p password 123456 创建用户只能在10.0.0.0网段下访问数据库grant select,create,insert,update o ...

  9. 利用wireshark和python分析网络

  10. 第三章:ionic环境搭建之windows篇

    下面是在windows操作系统上面安装ionic的步骤,已经在Windows 10/ 7/ XP下面通过验证. 安装JDK 1.1 下载(http://www.oracle.com/technetwo ...