Everyone said that the timer is not very good, often encounter: if a task in front of the slow, beyond the period, then the next time the timer poll will not be delayed.
Colleagues say Scheduleexecutorservice can avoid the problem, I write an example test under:
Package Com.dx.testparallel;import Java.util.concurrent.executors;import Java.util.concurrent.scheduledexecutorservice;import Java.util.concurrent.TimeUnit; Public classTesttimer { Public Static voidMain (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's executions'll commence after//InitialDelay then Initialdelay+period and then InitialDelay + 2 *//period, and so on. If any execution of the task encounters an//exception, subsequent executions is suppressed. //Otherwise, the task would only terminate via cancellation or//termination of the executor. If any execution of the This task//takes longer than its period and then subsequent executions may//start late, but would not concurrently execute.Scheduledexecutorservice.scheduleatfixedrate (NewThetask (),0, +, Timeunit.milliseconds); }}
PackageCom.dx.testparallel;ImportJava.sql.Timestamp;ImportJava.text.SimpleDateFormat;Importjava.util.Date;ImportJava.util.UUID;Importjavax.lang.model.element.VariableElement; Public classThetaskImplementsrunnable{@Override Public voidrun () {Timestamp TS=NewTimestamp (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 blockE.printstacktrace (); } TS=NewTimestamp (System.currenttimemillis ()); System.out.println ("Task" +uuid+ "End:" +ts.tostring ()); }}
Test Result: Why each time interval is 2s, not 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
Using Scheduledexecutorservice to implement a timer