Re: ScheduledExecutorService very inaccurate?
On 7/29/2010 11:04 AM, Chris Seidel wrote:
On Thu, 29 Jul 2010 19:49:30 +0200, Lew <lew@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/>