2011-06-10 170 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