From: Daniel Pitts on
On 7/29/2010 11:04 AM, Chris Seidel wrote:
> 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.
Except you are reading it from a different thread, so there is a need to
sync. I doubt that is causing the problem, but it is still necessary.
Making the field volatile should be enough for this example.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
From: Lew on
Chris Seidel wrote:
>>> When I use java.util.Timer everything is ok:
>

Lew wrote:
>> By coincidence.
>

Chris Seidel wrote:
> Even when the Timer has only a single task and then gets terminated?
>

See the quotes I already cited upthread from Timer's Javadocs.

--
Lew

From: Chris Seidel on
On Thu, 29 Jul 2010 21:42:17 +0200, Lew <lew(a)lewscanon.com> wrote:

>> Even when the Timer has only a single task and then gets terminated?
>>
>
> See the quotes I already cited upthread from Timer's Javadocs.

OK, so a GC or too many threads and too many task at the same time can
delay the execution time.
From: Daniel Pitts on
On 7/29/2010 1:17 PM, Chris Seidel wrote:
> On Thu, 29 Jul 2010 21:42:17 +0200, Lew <lew(a)lewscanon.com> wrote:
>
>>> Even when the Timer has only a single task and then gets terminated?
>>>
>>
>> See the quotes I already cited upthread from Timer's Javadocs.
>
> OK, so a GC or too many threads and too many task at the same time can
> delay the execution time.
Or the OS process scheduler, or a disk-read, or a solar eclipse, or many
different things.

There is no guarantee of the latest it will run, only the earliest.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
From: Arne Vajhøj on
On 29-07-2010 14:02, Chris Seidel wrote:
> 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)?

Java can not really do much on a non-realtime OS.

Arne