Callable抛出异常与future.get
public class ThreadPoolTest {
@Test
public void testException(){
try{
testExecutorServiceException();
}catch(Exception e){
//e.printStackTrace();
System.out.println("fa");
}
}
public void testExecutorServiceException() throws Exception{
Boolean flag = true;
//线程池,一个线程,线程池内部的任务是异步串行执行
ExecutorService executorService = Executors.newSingleThreadExecutor();
//results中存放任务执行的结果
List<Future<Boolean>> results = new ArrayList<Future<Boolean>>(10);
try{
for(int i=0; i<10; i++){
Future<Boolean> future = executorService.submit(new ActSKUCacheCallable(i, flag));
/* if(false == future.get()){
//throw new Exception("4324");
//return;
}*/
//收集任务执行的结果,存储在results中
results.add(future);
}
//主线程中执行,所以flag的值不一定是异步任务处理后的结果
System.out.println("结果:" + flag);
for(int i=0; i<10; i++){
boolean flag1 = results.get(i).get();
System.out.println("执行结果:" + flag1);
}
}catch(Exception e){
//e.printStackTrace();
throw e;
}
}
private class ActSKUCacheCallable implements Callable<Boolean> {
int i;
Boolean flag;
ActSKUCacheCallable(int i, Boolean flag){
this.i = i;
this.flag = flag;
}
public Boolean call() throws Exception {
try {
if(i % 2 != 0){
throw new Exception("try");
}
System.out.println("正常执行");
} catch (Exception e) {
System.out.println("catch");
//flag = false;
//抛出异常不影响线程池中其他任务的执行
throw e;
//return Boolean.FALSE;
} finally {
System.out.println("finally 代码块" + flag);
}
System.out.println("try代码块外面");
return Boolean.TRUE;
}
}
}
执行结果:

Callable抛出异常与future.get的更多相关文章
- Callable接口和Future
本篇说明的是Callable和Future,它俩很有意思的,一个产生结果,一个拿到结果. Callable接口类似于Runnable,从名字就可以看出来了,但是Runnable不会返回结 ...
- 并发编程之Callable异步,Future模式
Callable 在Java中,创建线程一般有两种方式,一种是继承Thread类,一种是实现Runnable接口.然而,这两种方式的缺点是在线程任务执行结束后,无法获取执行结果.我们一般只能采用共享变 ...
- Java Callable接口与Future接口的两种使用方式
Java Callable.Future的两种使用方式Callable+Futurepublic class Test { public static void main(String[] args) ...
- 使用executor、callable以及一个Future 计算欧拉数e
package test; import java.math.BigDecimal; import java.math.MathContext; import java.math.RoundingMo ...
- Callable接口、Runable接口、Future接口
1. Callable与Runable区别 Java从发布的第一个版本开始就可以很方便地编写多线程的应用程序,并在设计中引入异步处理.Thread类.Runnable接口和Java内存管理模型使得多线 ...
- java核心知识点学习----创建线程的第三种方式Callable和Future CompletionService
前面已经指出通过实现Runnable时,Thread类的作用就是将run()方法包装成线程执行体,那么是否可以直接把任意方法都包装成线程执行体呢?Java目前不行,但其模仿者C#中是可以的. Call ...
- Callable与Future、FutureTask的学习 & ExecutorServer 与 CompletionService 学习 & Java异常处理-重要
Callable是Java里面与Runnable经常放在一起说的接口. Callable是类似于Runnable的接口,实现Callable接口的类和实现Runnable的类都是可被其他线程执行的任务 ...
- Android(java)学习笔记66:实现Runnable接口创建线程 和 使用Callable和Future创建线程
1. 前面说的线程的实现是新写一个子类继承Thread: 是将类声明为 Thread 的子类.该子类应重写 Thread 类的 run 方法.接下来可以分配并启动该子类的实例 2. 这里说的方案2是指 ...
- Callable与Future的简单介绍
Callable与Future的介绍 Callable与 Future 两功能是Java在后续版本中为了适应多并法才加入的,Callable是类似于Runnable的接口,实现Callable接口的类 ...
随机推荐
- springboot2.0配置连接池(hikari、druid)
springboot2.0配置连接池(hikari.druid) 原文链接:https://www.cnblogs.com/blog5277/p/10660689.html 原文作者:博客园--曲高终 ...
- xampp集成环境下重置mysql的密码
第一步:打开两个命令行工具,都进入到你的xampp安装目录下的mysql下的bin目录,如我安装的位置是D:xampp/mysql/bin: 第二步:在完成第一步的情况下,输入:mysqld --sk ...
- libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)
In Xcode 9 and Swift 4: Print exception stack to know the reason of the exception: Go to show break ...
- winform使用log4.net
因为我最近负责的Winform项目,好多都用到了这个log4net的日志功能,开发程序对数据一般都要求做到雁过留痕,所以日志对于我们程序员是不可或缺.因此我把对log4net的使用做一个记录总结,以便 ...
- 20175317 《Java程序设计》第七周学习总结
20175317 <Java程序设计>第七周学习总结 教材学习内容总结 第七周我学习了教材第八章的内容,学习了许多常用实用类,有以下内容: String类 1. 如何构造String对象 ...
- 2018-2019-2 网络对抗技术 20165303 Exp1 PC平台逆向破解(BOF实验)
1.实践目的 本次实践的对象是一个名为pwn1的linux可执行文件. 三个实践内容如下: 手工修改可执行文件,改变程序执行流程,直接跳转到getShell函数. 利用foo函数的Bof漏洞,构造一个 ...
- lua调用不同lua文件中的函数
a.lua和b.lua在同一个目录下 a.lua调用b.lua中的test方法,注意b中test的写法 _M 和 a中调用方法: b.lua local _M = {}function _M.test ...
- 使用js如何设置、获取盒模型的宽和高
第一种: dom.style.width/height 这种方法只能获取使用内联样式的元素的宽和高. 第二种: dom.currentStyle.width/height 这种方法获取的是浏览器渲染以 ...
- Element-ui表格选中回显
先瞄一下,是不是你要的效果 然后,废话不多说,直接上代码啦 <template> <div class> <div class="projectData&quo ...
- JVM工具jstat使用说明
输入:jstat -help得到以下帮助信息 Usage: jstat --help|-options jstat -<option> [-t] [-h<lines>] < ...