为什么TimerTask不能重用到另一个定时器中,但是已经使用过的TimerTask可以重用到ScheduledExecutorService中

Why TimerTask can't be reused into another timer, but already used TimerTask can be reused into ScheduledExecutorService

在下面的代码中,注释掉的部分导致了异常,向我们解释了 timerTask2 已经被安排或取消了,但是没有什么可以阻止 if 被执行者重用和 运行注释掉的部分下面的代码。如何解释?

Timer timer2 = new Timer();
TimerTask timerTask2 = new ThreadTest6();
delay = 1000 L;
long period = 1000 L;
timer2.scheduleAtFixedRate(timerTask2, delay, period);

try {
    Thread.sleep(10500);
} catch (InterruptedException e) {
    e.printStackTrace();
}

timer2.purge();
timer2.cancel();
timerTask2.cancel();

//        Timer timer3=new Timer();
//        System.out.println("Task reuse");
//        System.out.println();
//
//        timer3.scheduleAtFixedRate(timerTask2, delay, period);
//
//        try {
//            Thread.sleep(1500);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//

System.out.println("At the executor service");
System.out.println();

ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
delay = 1000 L;
period = 1000 L;
executor.scheduleAtFixedRate(timerTask2, delay, period, TimeUnit.MILLISECONDS);
try {
    Thread.sleep(delay + period * 3);
} catch (InterruptedException e) {
    e.printStackTrace();
}
executor.shutdown();

@Slaw 回答为:

因为 Timer 检查 TimerTask 是否已被取消(即此行为特定于 Timer)。对于ScheduledExecutorServiceTimerTask只是一个Runnable