I have a Service which is running on a thread. When I need the thread to stop running I am using this code
this.serviceThread.interrupt();
this.serviceThread = null;
At some point I need to recreate the thread again
this.serviceThread = new Thread()
{
public void run()
{
TheService.this.serviceProcessThread();
}
};
this.serviceThread.start();
However, it still seems like the previous Thread is still alive and running because it is listed in the list of currently running threads. This list just keeps growing every time I try to stop and create a new thread. Is this normal? Is there anyway I can get rid of those old threads?
I mainly just want to know if that list of threads means they are still there and, if so, how can I remove them. Thanks!
EDIT: This is how I am handling running/stopping the thread
public void startProcessThread()
{
this.shutdown = false;
this.serviceThread = new Thread()
{
public void run()
{
TheService.this.serviceProcessThread();
}
};
this.serviceThread.start();
}
private void serviceProcessThread()
{
do
{
try
{
this.getCommands();
if (this.tasks.size() > 0)
this.processTasks();
if (!this.shutdown)
{
Thread.sleep(ServiceSleepTime);
}
}
catch (Exception e)
{
this.logException("serviceProcessThread", e);
}
}
while (!this.shutdown);
if(this.serviceThread != null)
{
this.serviceThread.interrupt();
this.serviceThread = null;
}
}