定时器 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时间单位);
        }
}

 

posted @ 2019-07-28 13:47  笔记Next  阅读(361)  评论(0编辑  收藏  举报