每次创建新线程都被添加到主ThreadGroup

时间:2011-06-10 10:06:34

标签: android multithreading out-of-memory threadgroup

我正在创建许多表面视图实例来查看某些对象,但一次创建一个实例,然后调用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]

如何删除它们?

2 个答案:

答案 0 :(得分:1)

要寻找的一些事情。

  1. 您只在catch块中调用_thread = null - 这意味着如果永远不会抛出异常,则不会将其设置为null。将其移到finally块中。
  2. 可能永远不会抛出异常。调用Thread.interrupt()不会抛出它。这会将线程的中断标志设置为true。整个while循环很奇怪,你应该重写它。
  3. 您正在将此实例传递给BreedingThread。确保对象永远不会持有对表面视图的引用
  4. 简单地调用表面视图destroy方法并不意味着如果某些东西仍然存在对它的引用(例如我在3中提到的话),将删除引用。

答案 1 :(得分:0)

当你致电"新线程"并且不指定组,线程将被添加到与调用者相同的组(即Thread.currentThread().getThreadGroup())。在您的情况下,这是导致您的线程被添加到" main"线程组。要更改该行为,请使用new Thread(group, this)指定该组。