From: Chris Seidel on
Hi,

i'm using a ScheduledExecutorService to schedule a task. The problem is,
that the task gets execute about 10 - 20 percent too late, e.g. when I
schdule it with 20 s delay, it gets executed after 24 s.

Is this "normal"?

Here is the testcode:

package com.foo;

import static junit.framework.Assert.assertEquals;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.junit.Test;

public class SchedulerTest {
private ScheduledExecutorService s;
private long executedAtMillis;

@Test
public void test1() {
s = Executors.newScheduledThreadPool(1);
final long currentTimeMillis = System.currentTimeMillis();
long delay = 20000L;
s.schedule(new Runnable() {
public void run() {
executedAtMillis = System.currentTimeMillis();
}
}, delay, TimeUnit.MILLISECONDS);
try {
Thread.sleep(delay * 2);
} catch (InterruptedException e) {
}
assertEquals(delay, executedAtMillis - currentTimeMillis);
}
}

When I use java.util.Timer everything is ok:



@Test
public void test2() {
Timer t = new Timer();
final long currentTimeMillis = System.currentTimeMillis();
long delay = 20000L;

t.schedule(new TimerTask() {
@Override
public void run() {
executedAtMillis = System.currentTimeMillis();
}
}, delay);
sleep(delay);
assertEquals(delay, executedAtMillis - currentTimeMillis);
}

Thank you.
From: Lew on
Chris Seidel wrote:
> i'm [sic] using a ScheduledExecutorService to schedule a task. The problem is,  
> that the task gets execute about 10 - 20 percent too late, e.g. when I  
> schdule it with 20 s delay, it gets executed after 24 s.
>
> Is this "normal"?
>

Check the Javadocs. There is nothing there to promise that the
scheduled task will occur exactly at the termination of the delay or
even within any time frame thereafter. They only promise to "schedule
commands to run after a given delay", and your description shows that
that promise was kept.

There are two levels where it does not promise what you ask there.
The first is that it only promises to *schedule* the occurrence after
the delay, not to run it at the exact scheduled time. The second is
that the schedule is for *after* the delay, not for *when* the delay
expires.

Furthermore, java.util.Timer doesn't promise that kind of precision
either: "[java.util.Timer] does not offer real-time guarantees: it
schedules tasks using the Object.wait(long) method."
Object#wait(long) waits until "[t]he specified amount of real time has
elapsed, more or less. ... The thread T is then removed from the wait
set for this object and re-enabled for thread scheduling. It then
competes in the usual manner with other threads for the right to
synchronize on the object ...".

ScheduledExecutorService is an interface; its behavior is dependent on
some unknown implementation. Still, neither it nor Timer promises
what you're requesting.

--
Lew
From: Lew on
Chris Seidel wrote:
> When I use java.util.Timer everything is ok:
>

By coincidence.

>         @Test
>         public void test2() {
>                 Timer t = new Timer();
>                 final long currentTimeMillis = System.currentTimeMillis();
>                 long delay = 20000L;
>
>                 t.schedule(new TimerTask() {
>                         @Override
>                         public void run() {
>                                 executedAtMillis = System.currentTimeMillis();
>                         }
>                 }, delay);
>                 sleep(delay);
>                 assertEquals(delay, executedAtMillis - currentTimeMillis);
>         }
>

How are you synchronizing 'executedAtMillis'?

--
Lew
From: Chris Seidel on
On Thu, 29 Jul 2010 19:44:32 +0200, Lew <lew(a)lewscanon.com> wrote:

> ScheduledExecutorService is an interface; its behavior is dependent on
> some unknown implementation. Still, neither it nor Timer promises
> what you're requesting.

Do you know a Timer-Impl which guarantees the execution time (in that
sense that Java has no real time support)?

And do you know from where the quite constant offset comes, when using
ScheduledThreadPoolExecutor:

delay 2.000 > execution at 2.400
delay 20.000 > execution at 24.000
delay 200.000 > execution at 240.000
From: Chris Seidel on
On Thu, 29 Jul 2010 19:49:30 +0200, Lew <lew(a)lewscanon.com> wrote:

> Chris Seidel wrote:
>> When I use java.util.Timer everything is ok:
>>
>
> By coincidence.

Even when the Timer has only a single task and then gets terminated?

>> @Test
>> public void test2() {
>> Timer t = new Timer();
>> final long currentTimeMillis =
>> System.currentTimeMillis();
>> long delay = 20000L;
>>
>> t.schedule(new TimerTask() {
>> @Override
>> public void run() {
>> executedAtMillis =
>> System.currentTimeMillis();
>> }
>> }, delay);
>> sleep(delay);
>> assertEquals(delay, executedAtMillis -
>> currentTimeMillis);
>> }
>>
>
> How are you synchronizing 'executedAtMillis'?

Not at all, just a field var:

private long 'executedAtMillis';

There is only a single Timer-Thread running, no need to sync in this test.