ScheduledExecutorService学习笔记

来源:互联网 发布:监视网络流量软件 编辑:程序博客网 时间:2024/06/06 07:20

1.ScheduledExecutorService接口各实现类
ScheduledExecutorService接口各实现类
2.ScheduledExecutorService接口下的方法
ScheduledExecutorService接口下的方法


代码块:

ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(3);        System.out.println("调用开始...");        long start = System.currentTimeMillis();        scheduledExecutorService.scheduleWithFixedDelay(new Runnable() {            @Override            public void run() {                System.out.println("运行间隔..." + (System.currentTimeMillis() - start) / 1000 + "s");                System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));            }        }, 10l, 2l, TimeUnit.SECONDS); // 延迟十秒后,每隔两秒运行一次

运行结果:

调用开始...运行间隔...10s2016-12-06 23:55:25运行间隔...12s2016-12-06 23:55:27运行间隔...14s2016-12-06 23:55:29运行间隔...16s2016-12-06 23:55:31运行间隔...18s2016-12-06 23:55:33运行间隔...20s2016-12-06 23:55:35运行间隔...22s2016-12-06 23:55:37运行间隔...24s2016-12-06 23:55:39运行间隔...26s2016-12-06 23:55:41

0 0
原创粉丝点击