I have an app where when a "game" stats, it starts a couple different threads. I start the threads like so:
Thread thread = new Thread(new Runnable()
{
public void run()
{
//...
}
});
thread.setName("killMeAtEnd");
thread.start();
Later when the game ends i have a dispose() method inside of game which sorts through all of the running Threads and ends all of the Threads that have the name "killMeAtEnd". My question is, is this good practice? My intention is to keep my app running fast and clutter free, in my experience Threads that are left "hanging" tend to slow down the phone until the app is terminated. Is there a better way to do this? Is this even worth bothering about?
EDIT:
Here is my dispose()
if anyone was interested. This code is within the class Game.
public void dispose()
{
Thread threads[] = (Thread[])Thread.getAllStackTraces().keySet().toArray();
for(int x=0;x<threads.length;x++)
{
Thread thread = threads[x];
if(thread.getName().equalsIgnoreCase(KILL))
{
try
{
thread.interrupt();
}catch(Exception e){Log.e(Viewer.GAME,Log.getStackTraceString(e));}
thread = null;
}
}
}
public static final String KILL = "endOnDispose";