Thread is not removed after stopping
I have a Service that is running on a thread. When I need the thread to stop I use this codethis.serviceThread.interrupt();
this.serviceThread = null;
At some point I need to recreate the flow again
this.serviceThread = new Thread()
{
public void run()
{
TheService.this.serviceProcessThread();
}
};
this.serviceThread.start();
However, it still appears that the previous Thread is still alive and running because it is listed in the list of currently running threads. This list keeps growing every time I try to stop and create a new thread. This is normal? Is there anyway I can get rid of these old threads?
Basically I just want to know if this list of threads means they still exist, and if so, how can I delete them. Thank!
EDIT: This is how I process / stop 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;
}
}
source to share
The first thing you should run into is that a thread cannot be forcefully stopped without potentially adversely affecting the entire Java process. This is why Java introduced a thread abort mechanism: a general collaboration mechanism to gracefully stop a thread.
The co-location aspect is key: whatever you do in the stream implementation code, you must ensure that it breaks. Short checklist:
-
if you have blocking calls (those that stop the thread while waiting for a condition), they must be interrupt (basically, declare throw
InterruptedException
); -
you should handle correctly and correctly by
InterruptedException
doing any cleanup and returning the top level methodrun
, -
If you've done a long loop, you should make sure it checks the flag periodically
Thread.currentThread().isInterrupted()
and aborts if the thread is interrupted; -
if you have lost control of any third party code, make sure that code breaks.
Also keep in mind that the lifecycle Thread
as a Java object has nothing to do with the actual flow of execution. Thread
is just a descriptor object that allows control of the main stream, since it File
is a descriptor for a file system object. Setting a link File
to null
does not remove the file from the system.
Update
Your implementation code can be eliminated without swallowing InterruptedException
.
try {
this.getCommands();
if (this.tasks.size() > 0)
this.processTasks();
if (!this.shutdown)
Thread.sleep(ServiceSleepTime);
}
catch (InterruptedException e) {
this.shutdown = true;
}
catch (Exception e)
{
this.logException("serviceProcessThread", e);
}
Also, this part is redundant:
if(this.serviceThread != null)
{
this.serviceThread.interrupt();
this.serviceThread = null;
}
Here you are trying to interrupt your (current) thread. The point is that the thread must be interrupted from another thread.
source to share
... Interpreter () does not cause the stream to exit unless you check for interrupted status, e.g .:
if (Thread.interrupted()) {
skipRemainingActions();
}
or
while(!Thread.interrupted())
{
doStuff();
}
if there is still code to run on the thread, it will continue to run even if you call an interrupt on it
source to share
If you are not doing anything at the same time, then you need to create one thread and send it a job using a handler.
Here's a very good article: http://mindtherobot.com/blog/159/android-guts-intro-to-loopers-and-handlers/
You can also use AsyncTask to do the same thing - http://developer.android.com/reference/android/os/AsyncTask.html
If you have a task running at the same time you can use the executeOnExecutor () method AsyncTask refer- http://developer.android.com/reference/android/os/AsyncTask.html#executeOnExecutor(java.util.concurrent.Executor , Params ...)
source to share
Not a solution to your problem, but it doesn't do what you think:
Thread t = new Thread(new MyRunnableTask());
t.start();
t = null;
The call t.start()
creates a new thread. NOTE. I said "thread", not "Thread". A (big T) Thread is an object that can be used to create and manipulate a (little t) thread. Once a thread (little t) starts, it has a life of its own.
When you set the local variable t
to zero, all you do is delete your reference to the (big T) Thread object that manages the (little t) thread. The Thread object (big T) is unaffected: it will continue to exist as long as the thread (little t) is running. The thread (small t) is unaffected: it will continue to do whatever it does. The only thing that changes when set t
to null is that you no longer have any control over the new thread.
source to share