I am writing a servlet, which must start a thread in init() and correctly stop it after the server is stopped. Thread downloads a web page, parses it and stores data in MySQL database.
For stopping the thread I use servlet’s destroy() method. In this method I set condition for ending my thread. Is this the right way of doing it?
Can I see signs of the existence of my thread somewhere in my Windows? I didn’t find it in Windows Task Manager.
The Windows task manager only shows processes, not individual threads. In fact, JVM-managed threads fall in the category of User threads, which is a distinct category from Kernel threads (managed by the operating system).
To do what you want you will have to keep a global reference to your
Threadobject, such that it’s visible across different methods. It’s also preferable to make your thread spin on a flag and stop it by setting the flag, instead of using something likeThread.stop(). Something like this:And stop it by calling
setFlag(true).