栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JDK线程池异常被吞掉的设计思考

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JDK线程池异常被吞掉的设计思考

大家都知道提交任务到线程池有execute和submit两种方式,如果业务处理出异常了,前者会抛出堆栈信息,后者不会,至于为什么,大家可以去看看源码就知道了。

不过我这里有个疑问,就是Doug Lea大神为什么会设计成一种可以抛异常,一种不抛而是自己吞掉呢?

基于这段代码,我是这样想的

final void runWorker(Worker w) {
        Thread wt = Thread.currentThread();
        Runnable task = w.firstTask;
        w.firstTask = null;
        w.unlock(); // allow interrupts
        boolean completedAbruptly = true;
        try {
            while (task != null || (task = getTask()) != null) {
                w.lock();
                // If pool is stopping, ensure thread is interrupted;
                // if not, ensure thread is not interrupted.  This
                // requires a recheck in second case to deal with
                // shutdownNow race while clearing interrupt
                if ((runStateAtLeast(ctl.get(), STOP) ||
                     (Thread.interrupted() &&
                      runStateAtLeast(ctl.get(), STOP))) &&
                    !wt.isInterrupted())
                    wt.interrupt();
                try {
                    beforeExecute(wt, task);
                    Throwable thrown = null;
                    try {
                        task.run();
                    } catch (RuntimeException x) {
                        thrown = x; throw x;
                    } catch (Error x) {
                        thrown = x; throw x;
                    } catch (Throwable x) {
                        thrown = x; throw new Error(x);
                    } finally {
                        afterExecute(task, thrown);
                    }
                } finally {
                    task = null;
                    w.completedTasks++;
                    w.unlock();
                }
            }
            completedAbruptly = false;
        } finally {
            processWorkerExit(w, completedAbruptly);
        }
    }

processWorkerExit(w, completedAbruptly); 这句执行到的前提是:线程池中有线程处理任务出现异常了,为什么? 因为while (task != null || (task = getTask()) != null) 会一直阻塞嘛,它里面的逻辑就是把这个异常线程从woker中移除掉,好,也就是说必须抛了异常才能跑到这里,而execute方式如果异常了就能走到这里。

而submit提交方式是不会抛出异常的,因为它走的是下面这段代码,setException(ex)这句吞掉了异常, 代码在FutureTask中

public void run() {
        if (state != NEW ||
            !UNSAFE.compareAndSwapObject(this, runnerOffset,
                                         null, Thread.currentThread()))
            return;
        try {
            Callable c = callable;
            if (c != null && state == NEW) {
                V result;
                boolean ran;
                try {
                    result = c.call();
                    ran = true;
                } catch (Throwable ex) {
                    result = null;
                    ran = false;
                    setException(ex);
                }
                if (ran)
                    set(result);
            }
        } finally {
            // runner must be non-null until state is settled to
            // prevent concurrent calls to run()
            runner = null;
            // state must be re-read after nulling runner to prevent
            // leaked interrupts
            int s = state;
            if (s >= INTERRUPTING)
                handlePossibleCancellationInterrupt(s);
        }
    }

自然的也不会走到上面 processWorkerExit(w, completedAbruptly); 也就是这种提交任务方式不会因为异常而从woker中移除线程。

总结:
根据上面的分析,我个人理解为什么这样设计,是因为 submit 即使出现异常也不会从线程池中移除当前线程,而 execute 会移除,就是可以有两种选择,不知道这样理解的对不对,反正思考了就对了,请路过的大佬多指教~

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/666308.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号