zoukankan      html  css  js  c++  java
  • Timer 使用 (一)

    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 赶紧去阿里的开发手册 搜下 看有没有啥坑

  • 相关阅读:
    字符串数组
    常用函数
    判断是否是素数回文数
    杨辉三角
    惨痛的教训 没有 脑子的我
    剪缎带
    ?????函数不起作用
    C#3
    celery 原理和组件
    vue检查用户名是否重复
  • 原文地址:https://www.cnblogs.com/light-train-union/p/12029812.html
Copyright © 2011-2022 走看看