ScheduledThreadPoolExecutor定时线程池不执行原因分析

项目场景:

        公司同事封装了一个观察者模式,异步定时线程发送消息和更新Redis


问题描述

        公司同事封装了一个观察者模式,异步定时线程发送消息和更新Redis,但是Redis做了一次扩容,中间会有无法写入的异常,后来异步定时线程就不执行了

public DataChangedHandle(DataObservable dataObservable, SyncDataHandleConfigurer observer) {
        dataObservable.addObserver(observer);
        Executors.newScheduledThreadPool(2).scheduleWithFixedDelay(() -> {
            Iterator var1 = dataList.iterator();

            while(var1.hasNext()) {
                DataEntity object = (DataEntity)var1.next();
                dataObservable.setData(object);
                dataObservable.notifyObservers();
                dataList.remove(object);
            }

        }, 5L, 1L, TimeUnit.SECONDS);
    }

原因分析:

通过类图可以看出 ScheduledThreadPoolExecutor 继承 ThreadPoolExecutor线程池且实现了ScheduledExecutorService接口,

ScheduledThreadPoolExecutor 中的 scheduleWithFixedDelay方法延迟指定时间执行一次,之后按照:上一次任务执行时长 + 周期的时长 的时间去周期执行

 

 

ScheduledFutureTask继承FutureTask且实现RunnableScheduledFuture方法,ScheduledFutureTask重写了run方法,问题就出在这里,
ScheduledFutureTask.super.runAndReset()如果为true,
  1. setNextRunTime():设置下次为定期任务运行的时间
    
  2. reExecutePeriodic(outerTask):定期重新执行

其中执行的super.runAndRest方法内部,如果执行抛出异常,那么返回False,无法设置下次定时任务的时间和定期执行的方法,所以定时任务不执行了

 

原因小结:

当异常抛到ScheduledThreadPoolExecutor框架中时不进行下次调度时间的设置,从而导致ScheduledThreadPoolExecutor定时任务不调度。 


解决方案:

使用ScheduledThreadPoolExecutor的地方增加try-catch,异常捕获,不让异常抛到框架中即可解决

示例:

package com.hgq.thread.scheduled;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @description: ScheduledThreadPoolExecutor 执行周期性任务时,报错导致定时任务线程不执行
 * @author: hgq
 * @time: 2023/5/4 10:08
 */
public class ScheduledTask {
    public static final AtomicInteger count = new AtomicInteger(0);

    public static final ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(1, new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            Thread thread = new Thread(Thread.currentThread().getThreadGroup(), r, "sc-task");
            thread.setDaemon(true);
            return thread;
        }
    });

    public static void main(String[] args) throws InterruptedException {

        CountDownLatch latch  = new CountDownLatch(1);
        scheduler.scheduleWithFixedDelay(()->{
            System.out.println("starting scheduler "+count.get());
            if (count.get() == 5){
                throw new IllegalArgumentException("my Exception");
            }
            count.incrementAndGet();
        },0,1, TimeUnit.SECONDS);
        latch.await();
    }
}



执行结果:

 通过结果可以看出,定时任务执行到第6次抛出异常后,定时任务就不执行了

解决方案:

public static void main(String[] args) throws InterruptedException {

        CountDownLatch latch  = new CountDownLatch(1);
        scheduler.scheduleWithFixedDelay(()->{
            System.out.println("starting scheduler "+count.get());
            if (count.get() == 5){
                try {
                    //抛出异常不去捕获的话,ScheduledThreadPoolExecutor定时任务的线程会停止执行,
                    throw new IllegalArgumentException("my Exception");
                }catch (Exception e){
                    System.out.println(e);
                }
            }
            count.incrementAndGet();
        },0,1, TimeUnit.SECONDS);
        latch.await();
    }

增加代码异常捕获后,定时任务可以正常执行。

执行结果:

 

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值