今天看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 }