tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Bill Miller" <>
Subject RE: Terminating Timer Thread Gracefully
Date Tue, 12 Jul 2011 21:58:31 GMT
The problem is obviously that the thread within the Timer needs time to properly shutdown,
non-obvious part is "how long does it need, and how do you detect it's done?". Normally you
would do
a Thread.join() to ensure a thread has stopped before continuing, but as you mentioned before
don't have access to the true thread instance in this case. 

I'd say that the Thread.sleep() is doing the same as a Thread.join() would in this case, the
problem I see is that if your application is intended to run on different hardware the hardcoded
1000ms delay may not be long enough in some situations. :(

Have you checked to see if there are any methods available to indicate if the timer has completed
its shutdown? (My memory is unclear about this and the JavaDoc isn't handy either... maybe
another object you need to instantiate to control Timer objects??)

-----Original Message-----
From: Terence M. Bandoian [] 
Sent: July 12, 2011 3:47 PM
To: Tomcat Users List
Subject: Re: Terminating Timer Thread Gracefully

  Hi, Kris-

I tried using ScheduledExecutorService but ran into the same problem.  
After awaiting termination:


                 while ( !executorService.awaitTermination(
                     1, TimeUnit.SECONDS ) );

                 Thread.sleep( 1000 );
             catch ( InterruptedException ie )

I still had to insert a call to Thread.sleep to prevent the error 
message from being written to the logs.


-Terence Bandoian

On 1:59 PM, Kris Schneider wrote:
> On Tue, Jul 12, 2011 at 7:59 AM, Caldarale, Charles R
> <>  wrote:
>>> From: Terence M. Bandoian []
>>> 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
> But Timer doesn't expose its thread. An alternative would be use
> something like Executors.newSingleThreadScheduledExecutor() 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.
>>   - Chuck

To unsubscribe, e-mail:
For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message