首页 > 其他 > 详细

Timer 使用 (一)

时间:2019-12-12 18:00:50      阅读:88      评论:0      收藏:0      [点我收藏+]

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 使用 (一)

原文:https://www.cnblogs.com/light-train-union/p/12029812.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!