I am creating many surface view instances to view some objects but one instance is created at a time and then surfaceDestroyed is called. but the thread I am creating every time on surfacecreated is being added to main ThreadGroup.
Although I am interrupting and nullifying it but it still resides in main ThreadGroup and creating low memory exception.
Code Snippets: constructor
public class MsurfaceView extends SurfaceView implements
SurfaceHolder.Callback {
_thread = new mThread(this);
_thread.setName("mThread");
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (!_thread.isAlive()) {
_thread = new BreedingThread(this);
}
_thread.setRunning(true);
_thread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.d("mThread", "Surface Destroyed Called");
getHolder().removeCallback(this);
getHolder().addCallback(null);
boolean retry = true;
_thread.setRunning(false);
while (retry) {
try {
_thread.interrupt();
_thread.getThreadGroup().interrupt();
_thread.join();
retry = false;
} catch (InterruptedException e) {
Log.d("mThread", "Interrupted");
// pass interrupt exception
Thread.currentThread().interrupt();
Log.d("mThread", "b4 threadGroupInterrupted");
_thread.getThreadGroup().interrupt();
_thread.getThreadGroup().list();//this shows thread is in //list
_thread = null;
break;
}
}
}
UPDATE Thread.list function shows that my interrupted and null thread are still in threadgroup
06-10 15:22:52.780: INFO/System.out(1814): java.lang.ThreadGroup[name=main,maxPriority=10]
06-10 15:22:52.780: INFO/System.out(1814): Thread[main,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[Thread-2,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[Binder Thread #1,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[Binder Thread #2,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[FlurryAgent,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[AsyncTask #1,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[AsyncTask #2,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[Thread-17,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[mThread,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[mThread,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[mThread,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[Thread-38,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[Timer-2,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[mThread,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[mThread,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[Thread-53,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[Thread-286,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[Thread-327,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[Thread-359,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[mThread,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[Thread-409,5,main]
how to remove them ?