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

  • 相关阅读:
    新的知识点
    知识点
    9.14知识点
    列表内容
    css的背景和边框
    css的text和font
    css
    js 第一天
    浏览器的差距
    布局
  • 原文地址:https://www.cnblogs.com/light-train-union/p/12029812.html
Copyright © 2011-2022 走看看