1、TimerTask 内 run() 方法的异常并不会往外抛

System.out.println(Thread.currentThread().getName() + "___" + Thread.currentThread().getId());
Timer timer = new Timer();
// 延迟一秒后,每三分钟执行一次
timer.schedule(new MyTimerTask(timer), 1000, 1000 * 60 * 3);

private class MyTimerTask extends TimerTask {
private Timer timer;

public MyTimerTask(Timer timer) {
this.timer = timer;
}

@Override
public void run() {
try {
      System.out.println(Thread.currentThread().getName() + "___" + Thread.currentThread().getId());

if (1!=1) {
this.timer.cancel();
return;
}

       throw new Exception();
        
}
} catch (Exception e) {
Logger.info("xx");
}
}
}

打印线程id和线程name 发现run()方法非主线程 但 是走线程池的

2、存在一些缺陷 一些情况下不建议使用
参考1: https://www.cnblogs.com/jiliunyongjin/p/10313384.html

第一次在公司项目实战中使用Timer 赶紧去阿里的开发手册 搜下 看有没有啥坑

Timer 使用 (一)

 

 

 

 

相关文章:

  • 2022-12-23
  • 2021-08-16
  • 2021-12-16
  • 2021-09-29
  • 2022-03-03
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-02-15
  • 2021-09-26
  • 2021-10-26
  • 2021-10-11
  • 2021-09-06
  • 2021-07-22
相关资源
相似解决方案