以前的好像是用Timer类来实现的,后来的JDK有改善了:
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(10);
class RunnablTest implements Runnable
{
public void run()
{
System.out.println("ok");
}
}
// 5秒后执行任务,每隔2秒执行一次任务:打印"ok"字符串
executor.scheduleAtFixedRate(new RunnablTest(), 5, 2, TimeUnit.SECONDS);