首页 > 代码库 > 使用ScheduledExecutorService实现Timer
使用ScheduledExecutorService实现Timer
大家都说Timer不太好用,经常会遇到:如果前边的一个任务比较慢,超出了period,此时timer的下一次轮询也不会延迟。
同事说ScheduleExecutorService可以避免该问题,我写个例子测试下:
package com.dx.testparallel;import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.TimeUnit;;public class TestTimer { public static void main(String[] args){ ScheduledExecutorService scheduledExecutorService=Executors.newScheduledThreadPool(1); //Creates and executes a periodic action that becomes enabled //first after the given initial delay, and subsequently with the //given period; that is executions will commence after //initialDelay then initialDelay+period, then initialDelay + 2 * //period, and so on. If any execution of the task encounters an //exception, subsequent executions are suppressed. //Otherwise, the task will only terminate via cancellation or //termination of the executor. If any execution of this task //takes longer than its period, then subsequent executions may //start late, but will not concurrently execute. scheduledExecutorService.scheduleAtFixedRate(new TheTask(), 0,1000, TimeUnit.MILLISECONDS); }}
package com.dx.testparallel;import java.sql.Timestamp;import java.text.SimpleDateFormat;import java.util.Date;import java.util.UUID;import javax.lang.model.element.VariableElement;public class TheTask implements Runnable{ @Override public void run() { Timestamp ts = new Timestamp(System.currentTimeMillis()); UUID uuid = UUID.randomUUID(); System.out.println("task"+uuid+" start:"+ts.toString()); // long num=0;// for(int a=0;a<100000;a++){// for(int i=0;i<1000000;i++){// for(int j=0;j<1000000;j++){// num= a*i*j;// }// }// } try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } ts = new Timestamp(System.currentTimeMillis()); System.out.println("task"+uuid+" end:"+ts.toString()); }}
测试结果:为什么每次间隔时间是2s,而不是1s.
task418d088e-3288-4736-b40a-f3c4f496e2a2 start:2016-09-07 00:49:03.699task418d088e-3288-4736-b40a-f3c4f496e2a2 end:2016-09-07 00:49:05.836task741d2176-dbef-4f12-a73e-13a04ed5eb18 start:2016-09-07 00:49:05.836task741d2176-dbef-4f12-a73e-13a04ed5eb18 end:2016-09-07 00:49:07.836task96ebb6ca-c99b-482e-b608-bd560e89c7eb start:2016-09-07 00:49:07.836task96ebb6ca-c99b-482e-b608-bd560e89c7eb end:2016-09-07 00:49:09.837task1ce9f4bb-dd9e-4510-a697-73daef02fe6b start:2016-09-07 00:49:09.837task1ce9f4bb-dd9e-4510-a697-73daef02fe6b end:2016-09-07 00:49:11.837task0386a855-3e85-4d06-9d91-24abde228731 start:2016-09-07 00:49:11.837task0386a855-3e85-4d06-9d91-24abde228731 end:2016-09-07 00:49:13.838task886fe541-92d1-49bc-b5bd-2c425cf773ba start:2016-09-07 00:49:13.838task886fe541-92d1-49bc-b5bd-2c425cf773ba end:2016-09-07 00:49:15.838task460137d1-7daf-419b-b0d5-87f31833176c start:2016-09-07 00:49:15.838task460137d1-7daf-419b-b0d5-87f31833176c end:2016-09-07 00:49:17.838taskce78bffb-8151-46e8-81d9-57b01e9294a2 start:2016-09-07 00:49:17.838taskce78bffb-8151-46e8-81d9-57b01e9294a2 end:2016-09-07 00:49:19.838task1fad4b4f-4143-4098-943a-c894ad96ae58 start:2016-09-07 00:49:19.838task1fad4b4f-4143-4098-943a-c894ad96ae58 end:2016-09-07 00:49:21.838taskc5de2fab-0688-4410-9408-b0990cc76223 start:2016-09-07 00:49:21.838taskc5de2fab-0688-4410-9408-b0990cc76223 end:2016-09-07 00:49:23.838task7dccb95b-6088-4a5d-a2e0-edb4381f7d0c start:2016-09-07 00:49:23.838task7dccb95b-6088-4a5d-a2e0-edb4381f7d0c end:2016-09-07 00:49:25.838taskc05908b5-1bb2-496a-9ddc-c3939d783f2c start:2016-09-07 00:49:25.838taskc05908b5-1bb2-496a-9ddc-c3939d783f2c end:2016-09-07 00:49:27.839task251c4102-f214-4eb9-9e13-7ca7f2eb3362 start:2016-09-07 00:49:27.839task251c4102-f214-4eb9-9e13-7ca7f2eb3362 end:2016-09-07 00:49:29.839task645f6f0f-9d11-4545-be12-1feab67a6b06 start:2016-09-07 00:49:29.839task645f6f0f-9d11-4545-be12-1feab67a6b06 end:2016-09-07 00:49:31.84task82da54a5-6620-46f4-a840-10633555d2e8 start:2016-09-07 00:49:31.84task82da54a5-6620-46f4-a840-10633555d2e8 end:2016-09-07 00:49:33.84taskb33558c1-cb84-4724-85af-ac8060b169a0 start:2016-09-07 00:49:33.84
使用ScheduledExecutorService实现Timer
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。