Search code examples
androiduniversal-image-loaderinterrupted-exception

Task was Interrupted in Universal-Image-Loader


Recently I was using the Release 1.9.1 of UIL in my project And my tester feed back repeatedly that images didn't show up when the app was started first time but it's ok when the app was started or finished afterwards. Then I check the logs and logcat shows that "Task was Interrupted." And then I find the InterruptedException segment in the source codes. And the original codes are:

/** @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;
}

This code segment is in LoadAndDisplayImageTask of com.nostra13.universalimageloader.core. So in what situations will this exception be throwed out, bad link speed of internet, low memory in the device, busy CPU or anything else?


Solution

  • I think it can happens when you call ImageLoader.stop() or ImageLoader.destroy() or if system finished the application so app threads were interrupted. Meanwhile, the synchronized sentence in waitIfPaused prevent the same pic's url loading at the same time. So, if a url was loaded at the same time, the second one will wait util the first pic is loaded successfully.