2011-11-22 139 views
1

我有一個活動,創建一個工作線程轉換圖片列表中的圖片。是的,在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不會暫停一個線程。這是一個謎。

更新:我已將整個工作線程取出並放入由活動調用的單獨本地服務中。工作者線程在幾圈之後仍然消失。這太瘋狂了 - 工作者線程是服務的常見用法。可怕的事情正在發生......但我仍然感激有人給我一個線索,看看它是什麼。

回答

0

尤里卡!這根本不是線程問題。這是我的錯(驚喜!)。

我對原始代碼並不完全真實。它實際上是這樣的:

final long sleepLength = transitionIntervalManager.pollState(); 
try 
{ 
    Log.d(getLogTag(), "Ready to sleep for "+sleepLength); 
    Thread.sleep(sleepLength); 

transitionIntervalManager是看中類我已經寫了實際上隨着時間的推移緩慢過渡。我有一個錯誤,所以當它到達最後(最慢)的轉換時間時,調用pollState會進入無限循環並且不會返回。因爲該方法在第一個日誌之前被調用,所以日誌永遠不會被寫入 - 而我的線程仍然會試圖進行下一個轉換。 (難怪我的筆記本電腦風扇!)

我試圖簡化代碼,我沒有意識到我簡化了問題---我的意思是,這樣一段簡單的代碼怎麼會有錯誤。 ;)

0

我不知道爲什麼你的線程被殺死,但我的猜測是它與活動的生命週期有關。現在在Android中有一個很好的類,它允許輕鬆地線程化並定期將代碼發回UI界面。查看AsyncTask並使用publishProgress函數更新您的UI對象。再次我不確定你的代碼有什麼問題,但Android線程的最佳實踐是使用這個類。

+0

你確定*這是什麼建議在這種情況下?首先,AsyncTask建立在線程之上,就像我的線程一樣。其次,AsyncTasks是從一組線程中提取出來的 - 我們是否真的希望它的後臺方法每隔幾秒就會進入休眠狀態,並基本上永遠持續下去,從而剝奪了AsyncTask的池?最後,我們如何知道我的線程正在被殺死?在哪裏可以找到文檔,指出Android會簡單地殺死一個線程,而不中斷它,並在日誌中不提供任何內容來表明它是這樣做的? AsyncTask如何防止這種情況發生? –