For many reasons, it is not good practice to use threads inside a servlet.
java.util.Timer seems like wrapper around a thread. So, is it also not safe to use it?
If so, what is the safest way to schedule a task in a servlet?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes, you can use Timers.
One important thing to remember is to cancel that Timer when the servlet is stopped. If you forget to cancel the Timer, your webapp will suffer from memory leaks (classloader leaks, since the Timer’s Thread is bound to the WebappClassLoader via its ContextClassLoader) and cannot be deployed multiple times.