ScheduledExecutorService定时任务学习
scheduleAtFixedRate :每隔Xs执行任务
scheduleWithFixedDelay :上轮任务结束后的Xs后执行下次任务
如下是测试代码,就是at和with方法不同
public static void atFixed() { ScheduledExecutorService service = Executors.newScheduledThreadPool(3); long time = System.currentTimeMillis(); for( int i=0;i<1;i++) { final int j=i; Integer[] is = {1, 2, 3, 4, 5, 6, 7, 8, 9}; List<Integer> list = Arrays.asList(is); Iterator<Integer> it = list.iterator(); service.scheduleAtFixedRate(new Runnable() { int k=j; @Override public void run() { if (it.hasNext()) { System.out.println(k+"*****"+it.next() + "******" +this.hashCode()+"*****"+ (System.currentTimeMillis() - time)); try { //Thread.sleep(1000L); Thread.sleep(50L); } catch (InterruptedException e) { e.printStackTrace(); } } else { service.shutdown(); } // System.out.println(k+"无动作" + this.hashCode()); } }, 100, 100, TimeUnit.MILLISECONDS); } }
scheduleAtFixedRate的执行结果
0*****1******1949088124*****102 0*****2******1949088124*****203 0*****3******1949088124*****302 0*****4******1949088124*****403 0*****5******1949088124*****502 0*****6******1949088124*****602 0*****7******1949088124*****703 0*****8******1949088124*****804 0*****9******1949088124*****903
scheduleWithFixedDelay的执行结果
0*****1******732719140*****104 0*****2******732719140*****258 0*****3******732719140*****410 0*****4******732719140*****578 0*****5******732719140*****731 0*****6******732719140*****895 0*****7******732719140*****1048 0*****8******732719140*****1199 0*****9******732719140*****1352
但是如果执行的时间超过了等待时间的话,例如我把上面的sleep时间增加到500ms,
scheduleAtFixedRate的执行结果
0*****1******732719140*****102 0*****2******732719140*****602 0*****3******732719140*****1103 0*****4******732719140*****1604 0*****5******732719140*****2104 0*****6******732719140*****2604 0*****7******732719140*****3105 0*****8******732719140*****3606 0*****9******732719140*****4106
scheduleWithFixedDelay的执行结果
0*****1******602404570*****103 0*****2******602404570*****705 0*****3******602404570*****1308 0*****4******602404570*****1912 0*****5******602404570*****2515 0*****6******602404570*****3116 0*****7******602404570*****3717 0*****8******602404570*****4320 0*****9******602404570*****4921
修改几个参数
public static void atFixed() { ScheduledExecutorService service = Executors.newScheduledThreadPool(2); long time = System.currentTimeMillis(); for( int i=0;i<6;i++) { final int j=i; Integer[] is = {1, 2, 3, 4,}; List<Integer> list = Arrays.asList(is); Iterator<Integer> it = list.iterator(); service.scheduleWithFixedDelay(new Runnable() { int k=j; @Override public void run() { if (it.hasNext()) { System.out.println(k+"*****"+it.next() + "******" +this.hashCode()+"*****"+ (System.currentTimeMillis() - time)); try { //Thread.sleep(1000L); Thread.sleep(50L); } catch (InterruptedException e) { e.printStackTrace(); } } else { service.shutdown(); } // System.out.println(k+"无动作" + this.hashCode()); } }, 0, 1000, TimeUnit.MILLISECONDS); } }
scheduleAtFixedRate运行结果
0*****1******2062239825*****2
1*****1******874924232*****2
2*****1******987865148*****53
3*****1******1949088124*****53
4*****1******1054745528*****104
5*****1******1880302369*****104
0*****2******2062239825*****1003
1*****2******874924232*****1003
2*****2******987865148*****1054
3*****2******1949088124*****1054
4*****2******1054745528*****1105
5*****2******1880302369*****1105
0*****3******2062239825*****2004
1*****3******874924232*****2004
2*****3******987865148*****2055
3*****3******1949088124*****2055
5*****3******1880302369*****2106
4*****3******1054745528*****2106
0*****4******2062239825*****3003
1*****4******874924232*****3003
2*****4******987865148*****3054
3*****4******1949088124*****3054
4*****4******1054745528*****3105
5*****4******1880302369*****3105
scheduleWithFixedDelay的运行结果
1*****1******874924232*****2 0*****1******2062239825*****2 2*****1******987865148*****53 3*****1******1949088124*****53 4*****1******1880302369*****104 5*****1******1054745528*****104 1*****2******874924232*****1055 0*****2******2062239825*****1055 2*****2******987865148*****1106 3*****2******1949088124*****1108 5*****2******1054745528*****1157 4*****2******1880302369*****1159 0*****3******2062239825*****2108 1*****3******874924232*****2109 2*****3******987865148*****2159 3*****3******1949088124*****2160 5*****3******1054745528*****2210 4*****3******1880302369*****2211 0*****4******2062239825*****3159 1*****4******874924232*****3160 2*****4******987865148*****3210 3*****4******1949088124*****3211 5*****4******1054745528*****3261 4*****4******1880302369*****3262