我有一個活動,創建一個工作線程轉換圖片列表中的圖片。是的,在onPause()/ onResume()我啓動並停止工作線程。跳繩對工人的肉,我有一個循環,當它被中斷時結束,主要的變量設置爲空(或其他工作人員):Android神祕:線程消失?
public void run()
{
while(transitionWorkerThread == this)
{
try
{
Log.d(getLogTag(), "Ready to sleep.");
Thread.sleep(5000);
Log.d(getLogTag(), "ready to transition, is running? " + (transitionWorkerThread == this));
if(transitionWorkerThread == this)
{
Log.d(getLogTag(), "ready to post image change");
MAIN_THREAD_HANDLER.post(new Runnable()
{
@Override
public void run()
{
Log.d(getLogTag(), "ready to go to next image");
imageSlideView.goNextImage();
Log.d(getLogTag(), "supposedly went to next image");
}
});
Log.d(getLogTag(), "posted image change, is running? " + (transitionWorkerThread == this));
}
}
catch(final InterruptedException interruptedException)
{
Log.d(getLogTag(), "Worker interrupted.");
}
catch(final Throwable throwable)
{
Log.d(getLogTag(), "some sort of picture error?", throwable);
}
}
Log.d(getLogTag(), "dropped out of picture loop, worker: " + transitionWorkerThread + " is us? " + (transitionWorkerThread == this));
}
現在,這裏的奧祕:線程消失!它會在一段時間後停止執行!認真!看看這個日誌輸出:
11-22 13:13:40.186: D/PictureFrameActivity(941): Ready to sleep.
11-22 13:13:40.191: D/PictureFrameActivity(941): ready to go to next image
11-22 13:13:40.191: D/PictureFrameActivity(941): supposedly went to next image
11-22 13:13:45.253: D/PictureFrameActivity(941): ready to transition, is running? true
11-22 13:13:45.253: D/PictureFrameActivity(941): ready to post image change
11-22 13:13:45.253: D/PictureFrameActivity(941): posted image change, is running? true
11-22 13:13:45.253: D/PictureFrameActivity(941): Ready to sleep.
11-22 13:13:45.253: D/PictureFrameActivity(941): ready to go to next image
11-22 13:13:45.253: D/PictureFrameActivity(941): supposedly went to next image
11-22 13:13:50.272: D/PictureFrameActivity(941): ready to transition, is running? true
11-22 13:13:50.272: D/PictureFrameActivity(941): ready to post image change
11-22 13:13:50.272: D/PictureFrameActivity(941): ready to go to next image
11-22 13:13:50.272: D/PictureFrameActivity(941): supposedly went to next image
11-22 13:13:50.301: D/PictureFrameActivity(941): posted image change, is running? true
11-22 13:13:50.301: D/PictureFrameActivity(941): Ready to sleep.
11-22 13:13:55.325: D/PictureFrameActivity(941): ready to transition, is running? true
11-22 13:13:55.325: D/PictureFrameActivity(941): ready to post image change
11-22 13:13:55.325: D/PictureFrameActivity(941): ready to go to next image
11-22 13:13:55.325: D/PictureFrameActivity(941): supposedly went to next image
11-22 13:13:55.331: D/PictureFrameActivity(941): posted image change, is running? true
11-22 13:13:55.331: D/PictureFrameActivity(941): Ready to sleep.
11-22 13:14:00.011: E/dalvikvm(76): No free temp registers
11-22 13:14:00.011: E/dalvikvm(76): Jit: aborting trace compilation, reverting to interpreter
11-22 13:14:00.365: D/PictureFrameActivity(941): ready to transition, is running? true
11-22 13:14:00.365: D/PictureFrameActivity(941): ready to post image change
11-22 13:14:00.365: D/PictureFrameActivity(941): ready to go to next image
11-22 13:14:00.365: D/PictureFrameActivity(941): supposedly went to next image
11-22 13:14:00.371: D/PictureFrameActivity(941): posted image change, is running? true
11-22 13:14:06.042: D/dalvikvm(941): GC_CONCURRENT freed 2106K, 58% free 3260K/7751K, external 13519K/13938K, paused 15ms+10ms
11-22 13:14:06.541: I/dalvikvm(941): Jit: resizing JitTable from 1024 to 2048
11-22 13:14:11.111: D/dalvikvm(941): GC_CONCURRENT freed 2051K, 57% free 3342K/7751K, external 13519K/13938K, paused 19ms+17ms
11-22 13:14:15.121: D/dalvikvm(941): GC_CONCURRENT freed 2174K, 59% free 3216K/7815K, external 13519K/13938K, paused 20ms+20ms
11-22 13:14:19.271: D/dalvikvm(941): GC_CONCURRENT freed 2028K, 59% free 3235K/7815K, external 13519K/13938K, paused 24ms+21ms
你最後一次通知後「?張貼圖像的變化,正在運行真實的」,線程剛剛消失,並且從此杳無音信!看代碼,只有幾個選擇:我可以看到「工作中斷」或「某種圖像錯誤」,甚至「退出圖像循環」。如果一切順利,它應該循環回來並說「準備睡覺」。
但我沒有看到這個主題的其他內容!就好像它在循環結束時被暫停。當然,Android不會暫停一個線程。這是一個謎。
更新:我已將整個工作線程取出並放入由活動調用的單獨本地服務中。工作者線程在幾圈之後仍然消失。這太瘋狂了 - 工作者線程是服務的常見用法。可怕的事情正在發生......但我仍然感激有人給我一個線索,看看它是什麼。
你確定*這是什麼建議在這種情況下?首先,AsyncTask建立在線程之上,就像我的線程一樣。其次,AsyncTasks是從一組線程中提取出來的 - 我們是否真的希望它的後臺方法每隔幾秒就會進入休眠狀態,並基本上永遠持續下去,從而剝奪了AsyncTask的池?最後,我們如何知道我的線程正在被殺死?在哪裏可以找到文檔,指出Android會簡單地殺死一個線程,而不中斷它,並在日誌中不提供任何內容來表明它是這樣做的? AsyncTask如何防止這種情況發生? –