详解Java ScheduledThreadPoolExecutor的踩坑与解决方法

2022-10-22,,,,

概述

最近项目上反馈某个重要的定时任务突然不执行了,很头疼,开发环境和测试环境都没有出现过这个问题。定时任务采用的是scheduledthreadpoolexecutor,后来一看代码发现踩了一个大坑....

还原"大坑"

这个坑就是如果scheduledthreadpoolexecutor中执行的任务出错抛出异常后,不仅不会打印异常堆栈信息,同时还会取消后面的调度, 直接看例子。

@test
public void testexception() throws interruptedexception {
    // 创建1个线程的调度任务线程池
    scheduledexecutorservice scheduledexecutorservice = executors.newsinglethreadscheduledexecutor();
    // 创建一个任务
    runnable runnable = new runnable() {

        volatile int num = 0;

        @override
        public void run() {
            num ++;
            // 模拟执行报错
            if(num > 5) {
                throw new runtimeexception("执行错误");
            }
            log.info("exec num: [{}].....", num);
        }
    };

    // 每隔1秒钟执行一次任务
    scheduledexecutorservice.scheduleatfixedrate(runnable, 0, 1, timeunit.seconds);
    thread.sleep(10000);
}

运行结果:

  • 只执行了5次后,就不打印,不执行了,因为报错了
  • 任务报错,也没有打印一次堆栈,更导致调度任务取消,后果十分严重。

解决方案

解决方法也非常简单,只要通过try catch捕获异常即可。

运行结果:

看到不仅打印了异常堆栈,而且也会进行周期性的调度。

更推荐的做法

更好的建议可以在自己的项目中封装一个包装类,要求所有的调度都提交通过我们统一的包装类, 如下代码:

@slf4j
public class runnablewrapper implements runnable {
    // 实际要执行的线程任务
    private runnable task;
    // 线程任务被创建出来的时间
    private long createtime;
    // 线程任务被线程池运行的开始时间
    private long starttime;
    // 线程任务被线程池运行的结束时间
    private long endtime;
    // 线程信息
    private string taskinfo;

    private boolean showwaitlog;

    /**
     * 执行间隔时间多久,打印日志
     */
    private long durms = 1000l;

    // 当这个任务被创建出来的时候,就会设置他的创建时间
    // 但是接下来有可能这个任务提交到线程池后,会进入线程池的队列排队
    public runnablewrapper(runnable task, string taskinfo) {
        this.task = task;
        this.taskinfo = taskinfo;
        this.createtime = system.currenttimemillis();
    }

    public void setshowwaitlog(boolean showwaitlog) {
        this.showwaitlog = showwaitlog;
    }

    public void setdurms(long durms) {
        this.durms = durms;
    }

    // 当任务在线程池排队的时候,这个run方法是不会被运行的
    // 但是当任务结束了排队,得到线程池运行机会的时候,这个方法会被调用
    // 此时就可以设置线程任务的开始运行时间
    @override
    public void run() {
        this.starttime = system.currenttimemillis();

        // 此处可以通过调用监控系统的api,实现监控指标上报
        // 用线程任务的starttime-createtime,其实就是任务排队时间
        // 这边打印日志输出,也可以输出到监控系统中
        if(showwaitlog) {
            log.info("任务信息: [{}], 任务排队时间: [{}]ms", taskinfo, starttime - createtime);
        }

        // 接着可以调用包装的实际任务的run方法
        try {
            task.run();
        } catch (exception e) {
            log.error("run task error", e);
            throw e;
        }

        // 任务运行完毕以后,会设置任务运行结束的时间
        this.endtime = system.currenttimemillis();

        // 此处可以通过调用监控系统的api,实现监控指标上报
        // 用线程任务的endtime - starttime,其实就是任务运行时间
        // 这边打印任务执行时间,也可以输出到监控系统中
        if(endtime - starttime > durms) {
            log.info("任务信息: [{}], 任务执行时间: [{}]ms", taskinfo, endtime - starttime);
        }

    }
}

使用:

我们还可以在包装类里面封装各种监控行为,如本例打印日志执行时间等。

原理探究

那大家有没有想过为什么任务出错会导致异常无法打印,甚至调度都取消了呢?让我们从源码出发,一探究竟。

1.下面是调度任务的入口方法。

// scheduledthreadpoolexecutor#scheduleatfixedrate
public scheduledfuture<?> scheduleatfixedrate(runnable command,
                                              long initialdelay,
                                              long period,
                                              timeunit unit) {
    if (command == null || unit == null)
        throw new nullpointerexception();
    if (period <= 0)
        throw new illegalargumentexception();
    // 将执行任务和参数包装成scheduledfuturetask对象
    scheduledfuturetask<void> sft =
        new scheduledfuturetask<void>(command,
                                      null,
                                      triggertime(initialdelay, unit),
                                      unit.tonanos(period));
    runnablescheduledfuture<void> t = decoratetask(command, sft);
    sft.outertask = t;
    // 延迟执行
    delayedexecute(t);
    return t;
}

这个方法主要做了两个事情:

  • 将执行任务和参数包装成scheduledfuturetask对象
  • 调用delayedexecute方法延迟执行任务

2.延迟或周期性任务的主要执行方法, 主要是将任务丢到队列中,后续由工作线程获取执行。

// scheduledthreadpoolexecutor#delayedexecute
private void delayedexecute(runnablescheduledfuture<?> task) {
        if (isshutdown())
            reject(task);
        else {
            // 将任务丢到阻塞队列中
            super.getqueue().add(task);
            if (isshutdown() &&
                !canrunincurrentrunstate(task.isperiodic()) &&
                remove(task))
                task.cancel(false);
            else
                // 开启工作线程,去执行任务,或者从队列中获取任务执行
                ensureprestart();
        }
    }

3.现在任务已经在队列中了,我们看下任务执行的内容是什么,还记得前面的包装对象scheduledfuturetask类,它的实现类是scheduledfuturetask,继承了runnable类。

// scheduledfuturetask#run方法
public void run() {
    // 是不是周期性任务
    boolean periodic = isperiodic();
    if (!canrunincurrentrunstate(periodic))
        cancel(false);
    // 不是周期性任务的话, 直接调用一次下面的run    
    else if (!periodic)
        scheduledfuturetask.super.run();
    // 如果是周期性任务,则调用runandreset方法,如果返回true,继续执行
    else if (scheduledfuturetask.super.runandreset()) {
        // 设置下次调度时间
        setnextruntime();
        // 重新执行调度任务
        reexecuteperiodic(outertask);
    }
}

这里的关键就是看scheduledfuturetask.super.runandreset()方法是否返回true,如果是true的话继续调度。

4.runandreset方法也很简单,关键就是看报异常如何处理。

// futuretask#runandreset
protected boolean runandreset() {
    if (state != new ||
        !unsafe.compareandswapobject(this, runneroffset,
                                     null, thread.currentthread()))
        return false;
    // 是否继续下次调度,默认false
    boolean ran = false;
    int s = state;
    try {
        callable<v> c = callable;
        if (c != null && s == new) {
            try {
                // 执行任务
                c.call(); 
                // 执行成功的话,设置为true
                ran = true;

                // 异常处理,关键点
            } catch (throwable ex) {
                // 不会修改ran的值,最终是false,同时也不打印异常堆栈
                setexception(ex);
            }
        }
    } 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
        s = state;
        if (s >= interrupting)
            handlepossiblecancellationinterrupt(s);
    }
    // 返回结果
    return ran && s == new;
}
  • 关键点ran变量,最终返回是不是下次继续调度执行
  • 如果抛出异常的话,可以看到不会修改ran为true。

总结

java的scheduledthreadpoolexecutor定时任务线程池所调度的任务中如果抛出了异常,并且异常没有捕获直接抛到框架中,会导致scheduledthreadpoolexecutor定时任务不调度了。这个结论希望大家一定要记住,不然非常坑,关键是有时候测试环境、开发环境还无法复现,有一定的随机性,真的到了生产就完蛋了。

到此这篇关于详解java scheduledthreadpoolexecutor的踩坑与解决方法的文章就介绍到这了,更多相关java scheduledthreadpoolexecutor内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

《详解Java ScheduledThreadPoolExecutor的踩坑与解决方法.doc》

下载本文的Word格式文档,以方便收藏与打印。