我应该如何恰当地关闭毕加索?
实施例: 我只有1个活动,在onDestroy中的这个活动中,我在所有picasso实例上调用shutdown()(我不使用单例实例)。 但是,在此活动被破坏之前,毕加索正在让设备保持清醒状态(我启动应用程序,使用它,按下主页,单独留下手机,周一检查,电池坏了,因为毕加索保持手机清醒)
这些Picasso线程仍在运行:
-Picasso-统计
-Picasso-refQue(两次)
-Picasso-Dispatcher(两次)
为什么呢?他们应该这样做吗?
关闭它的最佳做法是什么?在onStop()?我应该保留一份未完成的下载列表,我可能想在onResume()中重试?
答案 0 :(得分:0)
据我所知,picasso中的默认单例实例无法关闭,
但我解决了问题,在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;
}
}
}
虽然(true)有高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;
}
}
}