Java 使用 Timer 进行调度

版权声明:本文为大事龙原创文章,未经允许不得转载。 https://blog.csdn.net/w_yunlong/article/details/79924242
  • 入口程序
public class Main {
    public static void main(String[] args) {

        // Timer 调度
        Timer timer = new Timer();
        timer.schedule(new TimerDemo("first1"), 1000, 1000);
    }
}
  • 任务
public class TimerDemo extends TimerTask {
    private String name;
    public TimerDemo(String name) {
        this.name = name;
    }
    @Override
    public void run() {
        System.out.println("name = " + name);
    }
}

说明:
1. timer.schedule:
下次调度时间 = 任务上次执行完成时间+间隔时间
2. timer.scheduleAtFixedRate:
下次调度时间 = 任务上次执行开始时间+间隔时间

猜你喜欢

转载自blog.csdn.net/w_yunlong/article/details/79924242
今日推荐