测试一
- public static void method_02() {
- ScheduledExecutorService executor = Executors.newScheduledThreadPool(5);
-
- executor.scheduleAtFixedRate(new Runnable() {
- @Override
- public void run() {
- long start = new Date().getTime();
- System.out.println("scheduleAtFixedRate 开始执行时刻:" +
- DateFormat.getTimeInstance().format(new Date()));
- try {
- Thread.sleep(5000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- long end = new Date().getTime();
- System.out.println("scheduleAtFixedRate 执行耗费时刻=" + (end - start) / 1000 + "m");
- System.out.println("scheduleAtFixedRate 执行完成时刻:" + DateFormat.getTimeInstance().format(new Date()));
- System.out.println("======================================");
- }
- }, 1, 5, TimeUnit.SECONDS);
- }
执行功效

测试二

总结:以上两种方法差异的处所是使命的执行时刻,假如隔断时刻大于使命的执行时刻,使命不受执行时刻的影响。假如隔断时刻小于使命的执行时刻,那么使命执行竣事之后,会立马执行,至此隔断时刻就会被打乱。
测试一
- public static void method_03() {
- ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
-
- executor.scheduleWithFixedDelay(new Runnable() {
- @Override
- public void run() {
- long start = new Date().getTime();
- System.out.println("scheduleWithFixedDelay 开始执行时刻:" +
- DateFormat.getTimeInstance().format(new Date()));
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- long end = new Date().getTime();
- System.out.println("scheduleWithFixedDelay执行耗费时刻=" + (end - start) / 1000 + "m");
- System.out.println("scheduleWithFixedDelay执行完成时刻:"
- + DateFormat.getTimeInstance().format(new Date()));
- System.out.println("======================================");
- }
- }, 1, 2, TimeUnit.SECONDS);
- }
(编辑:湖南网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|