一、采用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. POJ_3090 Visible Lattice Points 【欧拉函数 + 递推】

    一.题目 A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), ...

  2. AtCoder Beginner Contest 113 B

    B - Palace Time limit : 2sec / Memory limit : 1024MB Score: 200 points Problem Statement A country d ...

  3. HDU - 1223 DP 分类

    据说这个是经典问题 \(dp[i][j]=dp[i-1][j-1]*j+dp[i-1][j]*j\) \(dp[i][j]\)表示前i个数分为j个集合,[i-1][j-1]为插入小于号[i-1][j] ...

  4. 对于position:relative,absolute,fixed的见解:

    1.switch--fixed,div脱离父元素,top,left,right,bottom都是相对于body,自己原来的位置不存在,即不占父元素位置了 2.switch--relative,div相 ...

  5. 【Python】探测网站是否可以访问

    首先贴上简陋的python脚本 #coding:utf-8 import urllib,linecache for line in linecache.updatecache('url.txt'): ...

  6. python 爬虫系列06--古诗文

    读书破万卷,下笔如有神 import requests import re def parse_page(url): headers = { 'USer-Agent':'user-agent: Moz ...

  7. (转)2017年最新企业面试题之shell(一,二)

    2017年最新企业面试题之shell(一) ********************************************** 企业Shell面试题1:批量生成随机字符文件名案例 * *** ...

  8. c++ MFC图像处理CImage类常用操作代码

    原文作者:aircraft 原文地址:https://www.cnblogs.com/DOMLX/p/9598974.html MFC图像处理CImage类常用操作 CImage类头文件为#inclu ...

  9. 3d旋转卡牌

    做成向中心缩放就行了,和旋转效果一样的

  10. [转]Wrapping multiple calls to SaveChanges() in a single transaction

    本文转自:http://www.binaryintellect.net/articles/165bb877-27ee-4efa-9fa3-40cd0cf69e49.aspx When you make ...