java try后没有catch,只有finnally

今天看jdk1.6源码  ThreadPoolExecutor中Worker的runTask方法   

catch(RunTimeException ex) 中 tthrow ex,会把ex抛到上层,上层try没有catch异常,该异常还会往上层抛,

try后直接跟finnally,finnally中runLock.unlock(),会释放锁;

总结:try....finnally 的用法主要是为了释放资源,不进行异常捕获,将异常交由上层调用者处理;

1private void runTask(Runnable task) { 2 final ReentrantLock runLock = this.runLock; 3 runLock.lock(); 4 try { 5 /* 6 * If pool is stopping ensure thread is interrupted; 7 * if not, ensure thread is not interrupted. This requires 8 * a double-check of state in case the interrupt was 9 * cleared concurrently with a shutdownNow -- if so, 10 * the interrupt is re-enabled. 11 */ 12 if ((runState >= STOP || 13 (Thread.interrupted() && runState >= STOP)) && 14 hasRun) 15 thread.interrupt(); 16 /* 17 * Track execution state to ensure that afterExecute 18 * is called only if task completed or threw 19 * exception. Otherwise, the caught runtime exception 20 * will have been thrown by afterExecute itself, in 21 * which case we don't want to call it again. 22 */ 23 boolean ran = false; 24 beforeExecute(thread, task); 25 try { 26 task.run(); 27 ran = true; 28 afterExecute(task, null); 29 ++completedTasks; 30 } catch (RuntimeException ex) { 31 if (!ran) 32 afterExecute(task, ex); 33 throw ex; 34 } 35 } finally { 36 runLock.unlock(); 37 } 38 }
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

PHP 异常处理

//报错,扑捉不到异常//try{//require('test_try_catch.php');//}catch(Exception$e){//echo$egetMessage();//}//可以扑捉到异常tr

Java中return返回结果的优先级

在Java开发时,异常处理是非常普遍的。先看这样一道关于异常处理的代码publicstaticintgetNumer(){inta1;try{returna;}catch(Exceptione){

Python从入门到入土

异常处理tryexcept在Python中,异常处理,主要是tryexcept语句,通常语法格式如下.try:代码块1exceptExceptionase:print(e)代码2try语句按照如下方式工作;首先,执行try子句(在关键字try和关键字except之间的语句)如果没有异常