I have an application where, when the "game statistics", it launches a couple of different threads. I start topics like this:
Thread thread = new Thread(new Runnable() { public void run() {
Later, when the game ends, I have a dispose () method inside the game that sorts all current threads and ends all threads that have the name "killMeAtEnd". My question is, is this a good practice? My intention is for my application to work quickly and randomly, in my experience. Topics that remain “hanging” tend to slow down the phone until the application is terminated. Is there a better way to do this? Is it even worth the worry?
EDIT:
Here is my dispose() if anyone is interested. This code is in the Game class.
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";
John
source share