本文介绍了每创建新线程的时间被添加到主线程组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创造了许多表面视图实例,查看一些对象,但在同一时间被创建一个实例,然后surfaceDestroyed被调用。但我在创建每个surfacecreated时间线被添加到主线程组。

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片段:
构造

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;
            }
        }
    }

更新
Thread.list功能表明,我的中断和零线仍处于线程组

UPDATEThread.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 ?

推荐答案

有些事情寻找。


  1. 您只调用_Thread = null,则catch块 - 这意味着它不会被设置为null,如果该异常抛出永远。此举在finally块。

  2. 该异常可能永远不会抛出。调用了Thread.interrupt()将不会扔掉它。这将线程的中断标志为true。这整个while循环是相当奇怪的,你或许应该重新写吧。

  3. 您正在传递这样的一个实例BreedingThread。确保对象不是抱着你的表面观的参考永远

  4. 简单地调用表面观销毁方法并不意味着会引用如果事情仍持有对它的引用(就像我在3比如提)被移除。

这篇关于每创建新线程的时间被添加到主线程组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 06:16