2015-11-04 83 views
1

我應該如何正確關閉畢加索?畢加索線程繼續運行

例如: 我只有1個活動,並且在onDestroy中的這個活動中,我在所有畢加索實例(我不使用單例實例)上調用shutdown()。 但是,直到這個活動被破壞,畢加索保持設備清醒(我啓動應用程序,使用它,按回家,離開電話週末一個人,週一檢查,電池已經死了,因爲畢加索保持手機清醒)。

這些畢加索線程仍在運行:

-Picasso-統計

-Picasso-refQue(兩次)

-Picasso一分配器(兩次)

爲什麼?他們應該嗎?

什麼是關閉它的最佳做法?在onStop()?我應該保留一份我可能想在onResume()中重試的未完成下載列表?

+0

如果您將活動保留在後臺,那麼您需要停止onStop或onPause中的所有內容。 –

+0

但是,如果它在下載過程中被取消(例如),則恢復所有內容的確很不方便。 – Automatick

+0

我不會讓畢加索保持設備自己清醒。很可能您正在不經意地從您的活動中提交新的圖片請求。無論如何,你可以關閉'onStop'中的所有東西。畢加索本身並不需要太多的清理,你可能想要取消掛起的請求,或通過調用'picasso.shutdown'來阻止進一步的請求。但不這樣做不足以讓您的設備在週末保持清醒! –

回答

0

據我所知默認畢加索單實例不能關機,

但我在行解決的問題,在「picasso.java」文件:643

發現這個代碼:

@Override public void run() { 
    Process.setThreadPriority(THREAD_PRIORITY_BACKGROUND); 
    while (true) { 
    try { 
     // Prior to Android 5.0, even when there is no local variable, the result from 
     // remove() & obtainMessage() is kept as a stack local variable. 
     // We're forcing this reference to be cleared and replaced by looping every second 
     // when there is nothing to do. 
     // This behavior has been tested and reproduced with heap dumps. 
     RequestWeakReference<?> remove = 
      (RequestWeakReference<?>) referenceQueue.remove(THREAD_LEAK_CLEANING_MS); 
     Message message = handler.obtainMessage(); 
     if (remove != null) { 
     message.what = REQUEST_GCED; 
     message.obj = remove.action; 
     handler.sendMessage(message); 
     } else { 
     message.recycle(); 
     }  
    } catch (InterruptedException e) { 
     break; 
    } catch (final Exception e) { 
     handler.post(new Runnable() { 
     @Override public void run() { 
      throw new RuntimeException(e); 
     } 
     }); 
     break; 
    } 
    } 
} 

,而(真)有CPU使用率很高,我決定改變如下:

@Override public void run() { 
    Process.setThreadPriority(THREAD_PRIORITY_BACKGROUND); 
    while (true) { 
    try { 
     // Prior to Android 5.0, even when there is no local variable, the result from 
     // remove() & obtainMessage() is kept as a stack local variable. 
     // We're forcing this reference to be cleared and replaced by looping every second 
     // when there is nothing to do. 
     // This behavior has been tested and reproduced with heap dumps. 
     RequestWeakReference<?> remove = 
      (RequestWeakReference<?>) referenceQueue.remove(THREAD_LEAK_CLEANING_MS); 
     Message message = handler.obtainMessage(); 
     if (remove != null) { 
     message.what = REQUEST_GCED; 
     message.obj = remove.action; 
     handler.sendMessage(message); 
     } else { 
     message.recycle(); 
     } 

     Thread.sleep(2000);//===> call ever 2 sec to decrease cpu pressure. 

    } catch (InterruptedException e) { 
     break; 
    } catch (final Exception e) { 
     handler.post(new Runnable() { 
     @Override public void run() { 
      throw new RuntimeException(e); 
     } 
     }); 
     break; 
    } 
    } 
}