【问题标题】:how to remove thread from threadgroup如何从线程组中删除线程
【发布时间】:2011-06-10 10:17:16
【问题描述】:

每次我创建新线程时,它都会被添加到主线程组中,即使我将线程归零,它仍然存在于主线程组中,从而导致内存泄漏。请帮忙

更新

public void surfaceDestroyed(SurfaceHolder holder) {
        Log.d("mThread", "Surface Destroyed Called");
        getHolder().removeCallback(this);
        boolean retry = true;
        _thread.setRunning(false);      
        while (retry) {
            try {
                Log.d("mThread", "b4 Interrupted");
                _thread.interrupt();
                Log.d("mThread", "b4 thread group Interrupted");
                _thread.getThreadGroup().interrupt();
                Log.d("mThread", "b4 join");
                _thread.join();
                retry = false;
            } catch (InterruptedException e) {
                Log.d("mThread", "Interrupted");
                Thread.currentThread().interrupt();
                _thread.getThreadGroup().list();                
                _thread = null;//======>here nulling thread
                break;
            }
        }
    }

【问题讨论】:

  • 我没有太多代码要显示。请帮忙
  • and even I null the thread it still exists in main ThreadGroup .. 这是什么意思?
  • 亲爱的跑者我已经更新了代码请帮助我
  • 任何正常(或异常但仍设法从本机代码调用exit())的线程都将从拥有的线程组中逐出。基本上你需要线程停止执行。
  • 我不能停止它已被弃用

标签: java android multithreading helper


【解决方案1】:

问题不在于它被添加到线程组中。已终止的线程将始终(最终)从线程组中删除。

如果应用程序正在泄漏内存,则您的代码中存在错误。你找错树了。

【讨论】:

  • 任何人都会引用,这与 threadGroup 无关。当一个线程退出时,它会从组中删除。就是这样。
  • @bestsss。谢谢,删除了那部分。
【解决方案2】:

如果线程存在于您的 ThreadGroup 中并且您需要删除它,您可以使用 ThreadGroup 类的.remove() 方法。它从 ThreadGroup 组中删除指定的线程。

语法:

void remove(Thread t);  
// t is thread to be removed from the ThreadGroup.

【讨论】:

  • 这个方法怎么用? Thread.currentThread.getThreadGroup() 不给我访问权限。它具有包级访问权限。我有自己的类扩展线程
  • @Aizaz :您需要在从 Thread.currentThread.getThreadGroup() 获得的 ThreadGroup 实例上调用 .remove(Thread t)
  • 它只是给了我 resume() 因为它是(包私有)void remove(Thread t)
  • @roadrunner,这是一个包私有方法,你不应该自己调用它(通过反射/setAccessible),不知道谁投票给了答案,但这没有意义。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-26
  • 2020-11-08
  • 1970-01-01
  • 2010-11-27
相关资源
最近更新 更多