tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Len Popp <len.p...@gmail.com>
Subject Re: Terminating Timer Thread Gracefully
Date Tue, 12 Jul 2011 15:40:04 GMT
On Tue, Jul 12, 2011 at 10:03, David kerber <dckerber@verizon.net> wrote:

> On 7/12/2011 9:59 AM, Kris Schneider wrote:
>
>> On Tue, Jul 12, 2011 at 7:59 AM, Caldarale, Charles R
>> <Chuck.Caldarale@unisys.com>  wrote:
>>
>>> From: Terence M. Bandoian [mailto:terence@tmbsw.com]
>>>> Subject: Terminating Timer Thread Gracefully
>>>>
>>>
>>>  Finally, in contextDestroyed, I inserted a call to
>>>> Thread.sleep after canceling the timer and the error
>>>> message disappeared.
>>>>
>>>
>>> You should be able to do a Thread.join() using the timer's Thread object
>>> rather than sleeping.
>>>
>>
>> But Timer doesn't expose its thread. An alternative would be use
>> something like Executors.**newSingleThreadScheduledExecut**or() to get a
>> ScheduledExecutorService. The executor can be used to schedule a
>> Runnable with a fixed rate or delay. When the context is destroyed,
>> shutdown the executor and await its termination.
>>
>
> No need even to do that; just .cancel() the timer.


Except that didn't work, according to Terence, until he added a sleep after
cancelling the timer. Could that be because the timer thread is busy
performing tasks that take some time to run? After cancel() is called the
timer thread "terminates gracefully" (according to the Java doc) which means
waiting for the currently executing task, if any, to finish.
-- 
Len

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message