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



我正在创建许多曲面视图实例来查看一些对象,但一次创建一个实例,然后调用surfaceDestroyed。但我每次在surfacecreated上创建的线程都被添加到主线程组中。

虽然我正在中断并取消它,但它仍然驻留在主线程组中,并创建低内存异常。

代码段:构造函数

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]

如何移除它们?

一些需要查找的内容。

  1. 您只在catch块内部调用_thread=null,这意味着如果从未抛出异常,它不会被设置为null。把它移到最后一块
  2. 该异常可能从未抛出。调用Thread.interrupt()不会抛出它。这会将线程的中断标志设置为true。整个while循环相当奇怪,您可能应该重写它
  3. 您正在将此实例传递给BreedingThread。确保该对象不会永远保持对曲面视图的引用
  4. 简单地调用surface view destroy方法并不意味着如果某个对象仍然包含对它的引用,则该引用将被删除(例如,如我在3中提到的)

当您调用"new Thread"而不指定组时,线程将被添加到与调用者相同的组中(即Thread.currentThread().getThreadGroup())。在您的情况下,这就是导致您的线程被添加到"主"线程组的原因。要更改该行为,请使用new Thread(group, this)指定组。

相关内容

  • 没有找到相关文章

最新更新