The problem is obviously that the thread within the Timer needs time to properly shutdown, the 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 you 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 only 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 there's another object you need to instantiate to control Timer objects??) Bill -----Original Message----- From: Terence M. Bandoian [mailto:tere...@tmbsw.com] 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: executorService.shutdown(); try { 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. Thanks. -Terence Bandoian On 1:59 PM, Kris Schneider wrote: > On Tue, Jul 12, 2011 at 7:59 AM, Caldarale, Charles R > <chuck.caldar...@unisys.com> wrote: >>> From: Terence M. Bandoian [mailto:tere...@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.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: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org