Java ScheduledExecutorService ScheduleWithFixedDelay adding unexpected delay after some iterations? -


i ran scheduling program on 2 m/c both windows different configuration-

public class testscheduling {     static boolean header = false;     static scheduledexecutorservice m_scheduleservice;     public static void main(string[] args) throws ioexception {          workerthread worker = new workerthread();         m_scheduleservice = executors.newscheduledthreadpool(1);          m_scheduleservice.schedulewithfixeddelay(worker, 1, 1,  timeunit.milliseconds);      }         static public class workerthread implements runnable{              public workerthread(){             }              @override             public void run() {                 try {                     processcommand();                 } catch (interruptedexception e) {                     // todo auto-generated catch block                     system.out.println("something wrong in thread");                     e.printstacktrace();                 }             }              private void processcommand() throws interruptedexception {                 date d = new date();                 system.out.println("print ...." + utility.getdatetostring(d));              }          } } 

on 1 m/c results -

print ....2016-01-28 15:42:45.289 print ....2016-01-28 15:42:45.289 print ....2016-01-28 15:42:45.289 print ....2016-01-28 15:42:45.289 print ....2016-01-28 15:42:45.289 print ....2016-01-28 15:42:45.289 print ....2016-01-28 15:42:45.289 print ....2016-01-28 15:42:45.299 print ....2016-01-28 15:42:45.299 print ....2016-01-28 15:42:45.299 print ....2016-01-28 15:42:45.299 print ....2016-01-28 15:42:45.299 

----after interval 10 ms of difference.

on m/c, result

print ....2016-01-28 05:06:54.239 print ....2016-01-28 05:06:54.239 print ....2016-01-28 05:06:54.239 print ....2016-01-28 05:06:54.239 print ....2016-01-28 05:06:54.239 print ....2016-01-28 05:06:54.239 print ....2016-01-28 05:06:54.255 print ....2016-01-28 05:06:54.255 print ....2016-01-28 05:06:54.255 print ....2016-01-28 05:06:54.255 print ....2016-01-28 05:06:54.255 print ....2016-01-28 05:06:54.255 

--second m/c after iteration shows difference 16 ms.

why there difference 2 different m/c? why there long delay after iteration? can unexpected delay removed?

you cannot expect scheduler(in 1 or more machines) run @ same time every time.

i have quoted below caution java docs of scheduledexecutorservice

beware expiration of relative delay need not coincide current date @  task enabled due network time synchronization protocols, clock drift,  or other factors. 

reference java doc link

ntp should able understand reason.


Comments

Popular posts from this blog

ios - UITEXTFIELD InputView Uipicker not working in swift -

Hatching array of circles in AutoCAD using c# -