2017-07-10 65 views
2

我正在嘗試獲取應用程序中的所有活動/正在運行的線程。我寫了下面的發佈代碼,並且得到了下面的發佈結果。 事實上,沒有在貼輸出線程屬於我的應用程序,除了如何在我的應用程序中獲取正在運行的線程

07-10 15:36:48.621 19333-19624/eu.men.m2sapp D/GcmIntentService: thread.toString():Thread[IntentService[GcmIntentService],5,main] 

1)是什麼數字5表示?

2)似乎'getAllStackTraces'檢索系統範圍內的所有活動線程,但是如何獲取僅在我的應用程序中運行的線程?

代碼

Set<Thread> threadSet = Thread.getAllStackTraces().keySet(); 
Thread[] threadArray = threadSet.toArray(new Thread[threadSet.size()]); 
Thread thread = null; 
for (int i = 0; i < threadArray.length; i++) { 
    thread = threadArray[i]; 
    Log.d(TAG, "thread.toString():" + thread.toString()); 
} 

輸出

07-10 15:36:48.621 19333-19624/eu.men.m2sapp D/GcmIntentService: thread.toString():Thread[IntentService[GcmIntentService],5,main] 
07-10 15:36:48.622 19333-19624/eu.men.m2sapp D/GcmIntentService: thread.toString():Thread[FinalizerWatchdogDaemon,5,system] 
07-10 15:36:48.622 19333-19624/eu.men.m2sapp D/GcmIntentService: thread.toString():Thread[Timer-0,5,main] 
07-10 15:36:48.622 19333-19624/eu.men.m2sapp D/GcmIntentService: thread.toString():Thread[RenderThread,5,main] 
07-10 15:36:48.622 19333-19624/eu.men.m2sapp D/GcmIntentService: thread.toString():Thread[ReferenceQueueDaemon,5,system] 
07-10 15:36:48.622 19333-19624/eu.men.m2sapp D/GcmIntentService: thread.toString():Thread[FinalizerDaemon,5,system] 
07-10 15:36:48.622 19333-19624/eu.men.m2sapp D/GcmIntentService: thread.toString():Thread[Binder_3,5,main] 
07-10 15:36:48.622 19333-19624/eu.men.m2sapp D/GcmIntentService: thread.toString():Thread[main,5,main] 
07-10 15:36:48.622 19333-19624/eu.men.m2sapp D/GcmIntentService: thread.toString():Thread[Signal Catcher,5,system] 
07-10 15:36:48.622 19333-19624/eu.men.m2sapp D/GcmIntentService: thread.toString():Thread[Binder_1,5,main] 
07-10 15:36:48.622 19333-19624/eu.men.m2sapp D/GcmIntentService: thread.toString():Thread[Binder_2,5,main] 
07-10 15:36:48.622 19333-19624/eu.men.m2sapp D/GcmIntentService: thread.toString():Thread[GCDaemon,5,system] 
07-10 15:36:48.622 19333-19624/eu.men.m2sapp D/GcmIntentService: thread.toString():Thread[pool-1-thread-1,5,main] 
07-10 15:36:48.622 19333-19624/eu.men.m2sapp D/GcmIntentService: thread.toString():Thread[JDWP,5,system] 
07-10 15:36:48.623 19333-19624/eu.men.m2sapp D/GcmIntentService: thread.toString():Thread[HeapTrimmerDaemon,5,system] 
07-10 15:36:48.623 19333-19624/eu.men.m2sapp D/GcmIntentService: thread.toString():Thread[Thread-52709,5,main] 

回答

3

其實,沒有在貼輸出線程屬於我的應用程序

所有這些線程都屬於您的應用程序的進程。大部分來自框架,而不是你的代碼。

似乎「getAllStackTraces」檢索所有活動線程全系統

不,檢索你的進程中的線程。

我怎樣才能得到只在我的應用程序中運行的線程?

你已經是。

5號表示什麼?

我相信這是線程優先。

+0

plus1。你能告訴我如何獲得從我的代碼開始的線程嗎? – user2121

+0

@ user2121:在啓動它們時跟蹤它們。從我所看到的,框架開始了你的所有線程,除非你決定創建自己的名爲'IntentService'的類。 – CommonsWare

+0

你能告訴我你是怎麼知道我從IntentService開始我的線程的嗎?好的,我們事先知道IntentService是Android類,但除了IntentService之外,應該知道一個線程是否從特定類開始?也許是因爲在課程名稱後面有'[]'方括號? – user2121

相關問題