On May 31, Mark Wielaard wrote:
> > But I think there is a race in Scheduler.run().  IMHO the queue
> > monitor must be kept between queue.top() and queue.sleep().  Otherwise
> > a task may be enqueued in between and the scheduler wouldn't notice
> > since the notify() comes before the sleep().
> Good catch. Now that you have pointed it out it seems so obvious.
> But I always wondered what a good method is to prevent or prove these
> kinds of race conditions. 

I don't know an ultimate rule, either.

> (You say IMHO, is that because you want to be
> friendly or because you cannot express the proof without any doubt?)

The second.  I had to be careful, as I only read the important
sections of your code.

> I will change the parameter to the sleep method so it represents the top
> TimerTask in the queue that you want to sleep on. Then sleep can check if
> the top task has changed and return immediatly if it has changed and do
> the real sleep otherwise (which should solve the race since sleep is
> synchronized).

I noticed later that there's a similar issue with the "stop" variable.
You should move it to the queue class so that the sleep() method can
check it, too.

Or maybe you put the whole sleep logic in Queue: You can add a
synchronized method to Queue, that sleeps until the first task in the
queue becomes ready, or the Timer was canceled.  It returns the task
and removes it from the queue, or returns null if the Timer was
canceled.

  Jochen

Reply via email to