一、采用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. [PowerShell]HTML parsing -- get information from a website

    link: http://stackoverflow.com/questions/9053573/powershell-html-parsing-get-information-from-a-webs ...

  2. R语言批量生成变量(变量名中含有参数)

    我们经常会需要生成这样一类的变量,比如a1,a2,a3...... 这时候我们需要用到这两个函数:get()和assign() get()用法 get()函数只是在环境中搜索该变量名的变量,如果该变量 ...

  3. .NET 海量数据并发处理

    .NET 海量数据并发处理 2013-02-19 10:27 提问者悬赏:10分 |XL码的小伙纸| 分类:C#/.NET| 浏览364次 百万级的数据并发处理,网站呈现数据..NET 开发,可能会使 ...

  4. notepad++配置编译运行java

    点击插件->Plugin Manager->show plugin manager : 选择NppExec,选择install,就将这个插件下载下来了. 这个时候会重启notepad++: ...

  5. Java static{}语句块详解

    [转自] http://blog.csdn.net/lubiaopan/article/details/4802430 static{}(即static块),会在类被加载的时候执行且仅会被执行一次,一 ...

  6. python--交互器,编译器

    1在写python的时候调交互器的作用 唯一作用:调试代码 2.编译器

  7. JS判断包括IE11在内的IE浏览器

    function isIE() { //ie? if (!!window.ActiveXObject || "ActiveXObject" in window) return tr ...

  8. Oracle RAC集群搭建(三)--挂载磁盘

    一,磁盘配置 查看由上回配置的共享磁盘,一共三块-----以下所有内容均两台物理机都需要操作 查看磁盘id [root@rac2 ~]# /usr/lib/udev/scsi_id -g -u /de ...

  9. C#DataTable与Model互转

    /// <summary> /// 实体转换辅助类 /// </summary> public class ModelConvertHelper<T> where ...

  10. Fastjson解析多级泛型的几种方式—使用class文件来解析多级泛型

    Fastjson解析多级泛型 前言 现在网上大多数是使用TypeReference 方式来解析JSON数据,这里我提供另外一种方式来解析,使用类文件进行解析,两种方式我都会给出实际代码 实例 Type ...