• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why System.currentTimeMillis() is later then TimerTask.scheduledExecutionTime() in some cases?  RSS feed

 
Sky Loi
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a TimerTask to trigger the transaction at every hours (e.g. 1:00, 2:00, 3:00, etc). As to the instruction of method TimerTask.scheduledExecutionTime(), I have the following codes to skip some overdue tasks:

public void run() {
if (System.currentTimeMillis() - scheduledExecutionTime() >= 1000)
return; // 1 s delay, Too late; skip this execution.
// Perform the task
}

But I encounter some strange issue which this code works in one of my server box but rather than the other server box (which running in VM). The issue is: when the task is triggered, e.g. 1:00, the scheduledExecutionTime() return kind of time 1:00, but the System.currentTimeMillis() return 1:01 (or a bit more, like 1:02), then the task is skipped as the checking above.

My questions are:
1. Will the System.currentTimeMillis() be delayed by the server performance? Is it related with VM server or non-VM server?
2. Will the TimerTask trigger process has delay?

Thank you very much for your help in advance.
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sky:

Actually, the issue is with Timer and TimerTask, not System.currentTimeMillis(). According to the Javadoc, Timer does not offer realtime guarantees.

John.
 
Sky Loi
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John de Michele wrote:Sky:

Actually, the issue is with Timer and TimerTask, not System.currentTimeMillis(). According to the Javadoc, Timer does not offer realtime guarantees.

John.


Hi, John, thanks for reply. I am just wondering why my existed two servers are always running ok (they have been running for several years) but the new install VM servers does not. Do you have any clue?

Thank you
 
Sky Loi
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is one interested thing that: once I startup the server initially, the TaskTimer trigger fine. But after that, the second, third and so on, the Timer runs behind. I cannot figure out what's happened.
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, John, thanks for reply. I am just wondering why my existed two servers are always running ok (they have been running for several years) but the new install VM servers does not. Do you have any clue?


Not without more information, no. Sorry.

There is one interested thing that: once I startup the server initially, the TaskTimer trigger fine. But after that, the second, third and so on, the Timer runs behind. I cannot figure out what's happened.


It's because the TimerTask runs on the same thread as the the Timer, so the next execution time is taken from the end of the TimerTask's execution. That means that if the Timer is set for execution every minute, and the TimerTask takes a second to run, the execution cycle would look like this:

1m...2m1s...3m2s...4m3s...etc

That doesn't take into account the fact that Timer isn't realtime, so it's likely that there will be additional variation.

John.
 
Sky Loi
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John de Michele wrote:
Hi, John, thanks for reply. I am just wondering why my existed two servers are always running ok (they have been running for several years) but the new install VM servers does not. Do you have any clue?


Not without more information, no. Sorry.



Great thanks, John. You help me have more clearer idea. I would like to address more detail of the strange case above: my TimerTask is set to run every hour per day (1:00, 2:00..). I put it in a JBoss service. The existed two servers are standalone redhat linux boxes, while the new installed redhat linux servers are running on virtual machine. Is it related with the server performance? or Java GC and running threads situation? Is it related with the System Time changing or configuring?

So far I use the workaround to just increase the delay time to 10 seconds to make the TimerTask running. However, I really want to figure out the root cause.

Thank you again.
 
Sky Loi
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is more information I found:

I use fixed-rate scheduling as the following method:
public void scheduleAtFixedRate(TimerTask task, Date firstTime, long period)

Tasks are triggered at every hour per day.

I found the difference of System.currentTimeMillis() and scheduledExecutionTime() increased gradually by 500ms and stops at about 12000ms and then keep stable. It happens in my VM linux server but is ok in my local desktop windows.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!