Skip to content

Commit

Permalink
Issue #187 - Be able to set the ExecutorService.
Browse files Browse the repository at this point in the history
Introduced ImageLoader.taskExecutor(...) and
.taskExecutorForCachedImages(...). Introduced ImageLoader.destroy().
Removed ImageLoader.stop(). Refactored warnings.
  • Loading branch information
nostra13 committed Mar 10, 2013
1 parent 11a606a commit d063841
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
package com.nostra13.universalimageloader.core;

import java.io.File;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import android.content.Context;
Expand All @@ -32,6 +37,8 @@
import com.nostra13.universalimageloader.cache.memory.impl.FuzzyKeyMemoryCache;
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.nostra13.universalimageloader.core.assist.MemoryCacheUtil;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.assist.deque.LIFOLinkedBlockingDeque;
import com.nostra13.universalimageloader.core.display.BitmapDisplayer;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
Expand All @@ -46,6 +53,13 @@
*/
public class DefaultConfigurationFactory {

/** Creates default implementation of task executor */
public static Executor createExecutor(int threadPoolSize, int threadPriority, QueueProcessingType tasksProcessingType) {
boolean lifo = tasksProcessingType == QueueProcessingType.LIFO;
BlockingQueue<Runnable> taskQueue = lifo ? new LIFOLinkedBlockingDeque<Runnable>() : new LinkedBlockingQueue<Runnable>();
return new ThreadPoolExecutor(threadPoolSize, threadPoolSize, 0L, TimeUnit.MILLISECONDS, taskQueue, createThreadFactory(threadPriority));
}

/** Creates {@linkplain HashCodeFileNameGenerator default implementation} of FileNameGenerator */
public static FileNameGenerator createFileNameGenerator() {
return new HashCodeFileNameGenerator();
Expand Down Expand Up @@ -94,7 +108,8 @@ public static BitmapDisplayer createBitmapDisplayer() {
return new SimpleBitmapDisplayer();
}

public static ThreadFactory createThreadFactory(int threadPriority) {
/** Creates default implementation of {@linkplain ThreadFactory thread factory} for task executor */
private static ThreadFactory createThreadFactory(int threadPriority) {
return new DefaultThreadFactory(threadPriority);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public class ImageLoader {

public static final String TAG = ImageLoader.class.getSimpleName();

static final String LOG_INIT_CONFIG = "Initialize ImageLoader with configuration";
static final String LOG_DESTROY = "Destroy ImageLoader";
static final String LOG_WAITING_FOR_RESUME = "ImageLoader is paused. Waiting... [%s]";
static final String LOG_RESUME_AFTER_PAUSE = ".. Resume loading [%s]";
static final String LOG_DELAY_BEFORE_LOADING = "Delay %d ms before loading... [%s]";
Expand All @@ -68,6 +70,8 @@ public class ImageLoader {
static final String LOG_TASK_INTERRUPTED = "Task was interrupted [%s]";
static final String LOG_CANT_DECODE_IMAGE = "Image can't be decoded [%s]";

private static final String WARNING_RE_INIT_CONFIG = "Try to initialize ImageLoader which had already been initialized before. "
+ "To re-init ImageLoader with new configuration call ImageLoader.destroy() at first.";
private static final String ERROR_WRONG_ARGUMENTS = "Wrong arguments were passed to displayImage() method (ImageView reference must not be null)";
private static final String ERROR_NOT_INIT = "ImageLoader must be init with configuration before using";
private static final String ERROR_INIT_CONFIG_WITH_NULL = "ImageLoader configuration can not be initialized with null";
Expand Down Expand Up @@ -96,8 +100,9 @@ protected ImageLoader() {
}

/**
* Initializes ImageLoader's singleton instance with configuration. Method should be called <b>once</b> (each
* following call will have no effect)<br />
* Initializes ImageLoader instance with configuration.<br />
* If configurations was set before ( {@link #isInited()} == true) then this method does nothing.<br />
* To force initialization with new configuration you should {@linkplain #destroy() destroy ImageLoader} at first.
*
* @param configuration {@linkplain ImageLoaderConfiguration ImageLoader configuration}
* @throws IllegalArgumentException if <b>configuration</b> parameter is null
Expand All @@ -107,8 +112,11 @@ public synchronized void init(ImageLoaderConfiguration configuration) {
throw new IllegalArgumentException(ERROR_INIT_CONFIG_WITH_NULL);
}
if (this.configuration == null) {
if (configuration.loggingEnabled) L.d(LOG_INIT_CONFIG);
engine = new ImageLoaderEngine(configuration);
this.configuration = configuration;
} else {
L.w(WARNING_RE_INIT_CONFIG);
}
}

Expand Down Expand Up @@ -444,9 +452,20 @@ public void resume() {
engine.resume();
}

/** Stops all running display image tasks, discards all other scheduled tasks */
public void stop() {
engine.stop();
/**
* Clears current configuration. Stops all running display image tasks, discards all other scheduled tasks (true for
* built-in task executors, false - for
* {@linkplain ImageLoaderConfiguration.Builder#taskExecutor(java.util.concurrent.ExecutorService) custom task
* executors}).<br />
* <br />
* You can {@linkplain #init(ImageLoaderConfiguration) init} ImageLoader with new configuration after calling this
* method.
*/
public void destroy() {
if (configuration != null && configuration.loggingEnabled) L.d(LOG_DESTROY);
engine.destroy();
engine = null;
configuration = null;
}

/**
Expand Down
Loading

0 comments on commit d063841

Please sign in to comment.