我正在创建许多表面视图实例来查看某些对象,但一次创建一个实例,然后调用surfaceDestroyed。但我每次在surfacecreated上创建的线程都被添加到主ThreadGroup。
虽然我正在中断并使其无效但它仍然驻留在主ThreadGroup中并且创建了低内存异常。
代码段: 构造
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;
}
}
}
更新 Thread.list函数显示我的中断和空线程仍在线程组
中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]
如何删除它们?
答案 0 :(得分:1)
要寻找的一些事情。
答案 1 :(得分:0)
当你致电"新线程"并且不指定组,线程将被添加到与调用者相同的组(即Thread.currentThread().getThreadGroup()
)。在您的情况下,这是导致您的线程被添加到" main"线程组。要更改该行为,请使用new Thread(group, this)
指定该组。