定时器 ScheduledExecutorService
public class ScheduleTest { private static int a=0; public static void main(String[] args) throws InterruptedException { ScheduledExecutorService execService = Executors.newSingleThreadScheduledExecutor(); execService.scheduleWithFixedDelay(new Runnable() { @Override public void run() { System.out.println(a++); if(a==5){ execService.shutdown(); } } }, 0, 1, TimeUnit.SECONDS); //(Runnable command线程代码,long initialDelay多长时间后运行任务,long delay任务结束后多长时间开始任务,TimeUnit unit时间单位); } }