From: Daniel Pitts on 29 Jul 2010 14:47 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 29 Jul 2010 15:42 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 29 Jul 2010 16:17 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 29 Jul 2010 16:27 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 29 Jul 2010 21:03 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
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 5 Prev: CyaSSL Java SSL Provider Alpha Next: How do I rewrite this in a cleaner way? |