2011-06-10 178 views
1

每次创建新的线程时,它都会被添加到主线程组中,甚至我会将它仍然存在于主线程组中的线程清零,从而导致内存泄漏。请帮助如何从线程组中删除线程

更新

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

我没有太多的代码显示。请帮助 – 2011-06-10 10:17:30

+0

',甚至我将它仍然存在于主线程组中的线程清空。这是什么意思? – 2011-06-10 10:24:15

+0

亲爱的走鹃我已经更新了代码,请帮助我 – 2011-06-10 10:36:06

回答

-2

如果线程线程组中的现有的和您需要将其删除,你可以使用ThreadGroup类的.remove()方法。它从ThreadGroup组中删除指定的线程。

语法:

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

如何使用这种方法? Thread.currentThread.getThreadGroup()不允许我访问。它具有包级访问权限。我有我自己的类扩展线程 – 2011-06-10 10:27:43

+0

@Aizaz:您需要在从Thread.currentThread.getThreadGroup()获得的ThreadGroup实例上调用'.remove(Thread t)'。 – 2011-06-10 10:29:01

+0

它只是给我简历(),因为它是(包私人)无效\t删除(线程t) – 2011-06-10 10:34:20

4

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

如果应用程序正在泄漏内存,则代码中存在一个错误。你在吠叫错误的树。

+0

请在这里看看http://stackoverflow.com/questions/6304879/every-time-creating-new-thread-is-being-added-to-main-threadgroup/6305122#6305122 – 2011-06-10 10:40:22

+0

*任何人都会引用*总是*,这与w/threadGroup无关。当线程退出时,它将从组中移除。就这样。 – bestsss 2011-06-10 11:00:22

+0

@bestsss。谢谢,删除了那部分。 – Kaj 2011-06-10 11:05:55