通用映像加载器中的任务已中断



最近我在我的项目中使用了UIL的1.9.1版本,我的测试人员反复反馈说,当应用程序第一次启动时,图像没有显示,但当应用程序启动或之后完成时,图像就可以了。然后我查看日志,logcat显示"Task was Interrupted"。然后我在源代码中找到InterruptedException段。原始代码是:

/** @return true - if task should be interrupted; false - otherwise */
private boolean waitIfPaused() {
    AtomicBoolean pause = engine.getPause();
    synchronized (pause) {
        if (pause.get()) {
            log(LOG_WAITING_FOR_RESUME);
            try {
                pause.wait();
            } catch (InterruptedException e) {
                L.e(LOG_TASK_INTERRUPTED, memoryCacheKey);
                return true;
            }
            log(LOG_RESUME_AFTER_PAUSE);
        }
    }
    return checkTaskIsNotActual();
}
/** @return true - if task should be interrupted; false - otherwise */
private boolean delayIfNeed() {
    if (options.shouldDelayBeforeLoading()) {
        log(LOG_DELAY_BEFORE_LOADING, options.getDelayBeforeLoading(), memoryCacheKey);
        try {
            Thread.sleep(options.getDelayBeforeLoading());
        } catch (InterruptedException e) {
            L.e(LOG_TASK_INTERRUPTED, memoryCacheKey);
            return true;
        }
        return checkTaskIsNotActual();
    }
    return false;
}

此代码段位于com.nostra13.univalimageloader.coreLoadAndDisplayImageTask中。那么,在什么情况下会抛出此异常、互联网链接速度不好、设备内存不足、CPU繁忙或其他任何情况?

这个问题可以通过以下方式解决:https://github.com/nostra13/Android-Universal-Image-Loader/issues/586

它看起来很棒。

我认为当您调用ImageLoader.stop()ImageLoader.destroy()时,或者如果系统完成了应用程序,因此应用程序线程中断,就会发生这种情况。同时,waitIfPaused中的synchronized语句阻止了同一图片的url同时加载。因此,如果同时加载了一个url,那么第二个url将等待第一个pic成功加载。

最新更新