指定一个线程任务,指定何时执行,指定每隔多久执行一次。其中,任务TimerTask是实现了Runnabe接口的。
package com.roocon.thread.t2; import java.util.Timer; import java.util.TimerTask; public class Demo5 { public static void main(String[] args) { Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { System.out.println("timer is running"); } }, 0, 1000); } }
运行结果:
timer is running
timer is running
timer is running
timer is running