2015-11-04 86 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; 
    } 
    } 
}