Race condition in TimerTask KillThread test

Alan Bateman Alan.Bateman at oracle.com
Fri Nov 4 22:37:49 UTC 2011


On 04/11/2011 15:52, Gary Adams wrote:
> :
>
> I'll attempt a simple fix for the KillThread case by replacing :
>
>     Thread.sleep(100);
>
> with a simple loop
>
>       do {
>            Thread.sleep(100);
>        } while (waiting);
>
> where the TimerTask runnable will  clear the flag
> with "waiting = false " once it has been launched.
>
I don't think this will guarantee that the timer thread will have 
terminated before the main thread schedules the second event. I think 
this test would be robust if you capture a reference to the timer thread 
in the first task and have the main thread wait or poll until it has a 
reference to the thread. Once it has a reference to the thread then it 
can wait for it to terminate before attempting to schedule the second task.

-Alan.



More information about the core-libs-dev mailing list