xc880216 2012-09-06 15:29
浏览 393
已采纳

java怎么再次设置Timer的schedule???

timer已经schedule过了。想在运行过程中更改触发频率。

  • 写回答

5条回答 默认 最新

  • 被掐死的温柔 2012-09-06 15:57
    关注

    public class TestTimer {

    class MyTimerTask extends TimerTask {
        private int time; //定时执行时间
    
        public MyTimerTask(){
    
        }
    
        public MyTimerTask(int _time){
            this.time = _time;
        }
    
        @Override
        public void run() {
            System.out.println("time:" + time);
            System.out.println((time==3000) );
            if(time==3000) {
                System.out.println("---------------");
                throw new RuntimeException("异常");
            }
            if(time%6000==0) {
                time = 0;
            }
            System.out.println("下次执行时间" + time);
            new Timer().schedule(new MyTimerTask(time+1000), time);
        }
    }
    public MyTimerTask init() {
        return new MyTimerTask();
    }
    public static void main(String[] args) throws Exception {
        new Timer().schedule(new TestTimer().init(), 2000);
        while(true) {
            System.out.println(new Date().getSeconds());
            Thread.sleep(1000);
        }
    
    }
    

    }

    不知道能不能满足你的要求

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型