先说结论 当一个Timer运行多个TimerTask时,只要其中一个TimerTask在执行中向run方法外抛出了异常,则其他任务也会自动终止。
我们看插件的提示
import java.util.Timer;
import java.util.TimerTask;
/**
* @author 小工匠
* @version 1.0
* @description: TODO
* @date 2021/11/21 20:28
* @mark: show me the code , change the world
*/
public class TimerTest {
// 定时器
static Timer timer = new Timer();
public static void main(String[] args) {
// 任务1 , 延迟500ms执行 1秒执行一次
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("Task1 Running");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 模拟发生异常
throw new RuntimeException();
}
},500,1000);
// 任务2, 延迟1000ms执行 1秒执行一次
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("Task2 Running");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},1000,1000);
}
}
如上代码首先添加了第一个任务,让其在500ms后执行。然后添加了第二个任务在1s后执行,我们期望当第一个任务输出Task1 Running后,等待1s,第二个任务输出Task1 Running,,然后循环,每隔1秒执行一次。
但是执行代码后,输出结果为
Timer的原理模型如下
从该实现模型我们知道,要探究上面的问题只需研究TimerThread的实现就可以了。TimerThread的run方法的主要逻辑代码如下。
/**
* This "helper class" implements the timer's task execution thread, which
* waits for tasks on the timer queue, executions them when they fire,
* reschedules repeating tasks, and removes cancelled tasks and spent
* non-repeating tasks from the queue.
*/
class TimerThread extends Thread {
/**
* This flag is set to false by the reaper to inform us that there
* are no more live references to our Timer object. Once this flag
* is true and there are no more tasks in our queue, there is no
* work left for us to do, so we terminate gracefully. Note that
* this field is protected by queue's monitor!
*/
boolean newTasksMayBeScheduled = true;
/**
* Our Timer's queue. We store this reference in preference to
* a reference to the Timer so the reference graph remains acyclic.
* Otherwise, the Timer would never be garbage-collected and this
* thread would never go away.
*/
private TaskQueue queue;
TimerThread(TaskQueue queue) {
this.queue = queue;
}
public void run() {
try {
mainLoop();
} finally {
// Someone killed this Thread, behave as if Timer cancelled
synchronized(queue) {
newTasksMayBeScheduled = false;
queue.clear(); // Eliminate obsolete references
}
}
}
/**
* The main timer loop. (See class comment.)
*/
private void mainLoop() {
while (true) {
try {
TimerTask task;
boolean taskFired;
// 从队列里面获取任务时要加锁
synchronized(queue) {
.........
if (taskFired) // Task fired; run it, holding no locks
task.run();// 执行任务
} catch(InterruptedException e) {
}
}
}
}
当任务在执行过程中抛出InterruptedException之外的异常时,唯一的消费线程就会因为抛出异常而终止,那么队列里的其他待执行的任务就会被清除。
所以在TimerTask的run方法内最好使用try-catch结构捕捉可能的异常,不要把异常抛到run方法之外。
其实要实现Timer功能,使用ScheduledThreadPoolExecutor
的schedule是比较好的选择。如果ScheduledThreadPoolExecutor
中的一个任务抛出异常,其他任务则不受影响。
public class TimerTest {
public static void main(String[] args) throws InterruptedException {
ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(1);
scheduledThreadPoolExecutor.schedule(()->{
System.out.println("Task1 Running");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 模拟发生异常
throw new RuntimeException();
},1, TimeUnit.SECONDS);
scheduledThreadPoolExecutor.schedule(()->{
System.out.println("Task2 Running");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 关闭线程池
scheduledThreadPoolExecutor.shutdown();
},1, TimeUnit.SECONDS);
}
}
之所以ScheduledThreadPoolExecutor的其他任务不受抛出异常的任务的影响,是因为在ScheduledThreadPoolExecutor中的ScheduledFutureTask任务中catch掉了异常 。
ScheduledThreadPoolExecutor是并发包提供的组件,其提供的功能包含但不限于Timer。Timer是固定的多线程生产单线程消费,但是ScheduledThreadPoolExecutor是可以配置的,既可以是多线程生产单线程消费也可以是多线程生产多线程消费,所以在日常开发中使用定时器功能时应该优先使用ScheduledThreadPoolExecutor。