项目场景:
公司同事封装了一个观察者模式,异步定时线程发送消息和更新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,
-
setNextRunTime():设置下次为定期任务运行的时间
-
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();
}
增加代码异常捕获后,定时任务可以正常执行。
执行结果: