码迷,mamicode.com
首页 > 编程语言 > 详细

java try后没有catch,只有finnally

时间:2020-01-08 19:06:19      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:vat   time   exe   boolean   current   exception   null   call   com   

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

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

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

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

 

        private void runTask(Runnable task) {
            final ReentrantLock runLock = this.runLock;
            runLock.lock();
            try {
                /*
                 * If pool is stopping ensure thread is interrupted;
                 * if not, ensure thread is not interrupted. This requires
                 * a double-check of state in case the interrupt was
                 * cleared concurrently with a shutdownNow -- if so,
                 * the interrupt is re-enabled.
                 */
                if ((runState >= STOP ||
                    (Thread.interrupted() && runState >= STOP)) &&
                    hasRun)
                    thread.interrupt();
                /*
                 * Track execution state to ensure that afterExecute
                 * is called only if task completed or threw
                 * exception. Otherwise, the caught runtime exception
                 * will have been thrown by afterExecute itself, in
                 * which case we don‘t want to call it again.
                 */
                boolean ran = false;
                beforeExecute(thread, task);
                try {
                    task.run();
                    ran = true;
                    afterExecute(task, null);
                    ++completedTasks;
                } catch (RuntimeException ex) {
                    if (!ran)
                        afterExecute(task, ex);
                    throw ex;
                }
            } finally {
                runLock.unlock();
            }
        }

java try后没有catch,只有finnally

标签:vat   time   exe   boolean   current   exception   null   call   com   

原文地址:https://www.cnblogs.com/fengyanqing/p/12168155.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!