ScheduledExecutorService starting late every time
I'm working on a small piece of code that has a ScheduledExecutorService.
To do that I have the following code:
Calendar fileTimestamp = Calendar.getInstance(); fileTimestamp.setTime(fileObj.getDateToCopy());
ScheduledExecutorService fileDispatcher = Executors.newScheduledThreadPool(5);
CopyTask copyTask = new CopyTask();
long schedulerTrigger = fileTimestamp.getTimeInMillis() - Calendar.getInstance().getTimeInMillis();
System.out.println("***[MAIN-INFO]["+fileObj.getFile_id()+"] fileTimeStamp:"+fileTimestamp.getTime());
System.out.println("***[MAIN-INFO]["+fileObj.getFile_id()+"] now
TimeStamp:"+Calendar.getInstance().getTime());
System.out.println("[MAIN-INFO]["+fileObj.getFile_id()+"]
scheduledTrigger [hh:mm:ss]:
"+formatDateDiff(schedulerTrigger) );
ScheduledFuture<?> result = fileDispatcher.schedule(copyTask,
schedulerTrigger, TimeUnit.MILLISECONDS);
The copyTask must me executed when we reach a timestamp. The problem is that 开发者_Python百科copyTask is executed very late: about 3 minutes late every time. Here below the messages:
***[MAIN-INFO][401850] fileTimeStamp:Tue May 17 17:09:38 CEST 2011
***[MAIN-INFO][401850] now TimeStamp:Tue May 17 17:07:38 CEST 2011
[MAIN-INFO][401850] scheduledTrigger [hh:mm:ss]: 0:2:0 (120000 ms)
The messages of the running task:
[401850-INFO] Launch time:Tue May 17 17:09:38 CEST 2011
[401850-INFO] Current time:Tue May 17 17:12:04 CEST 2011
[401850-INFO] Start copying FILE_ID=401850
[401850-INFO] Copy file OK
For the current time I have this code in the CopyTask:
public void run() {
System.out.println("["+taskId+"-INFO] Current time:"+new Date());
}
We can see that the launch time (the time when the job it must run) is not the same with the current time (the real time when the job is executed).
Does anybody have any idea why is running so late ?
Forget about it. Now it seems to work. I don't know why because I did nothing.
精彩评论