Skip to content

Commit

Permalink
Issue #89 - LIFO thread pool
Browse files Browse the repository at this point in the history
Added option to configuration to choose thread pool queue processing
type
  • Loading branch information
nostra13 committed Nov 1, 2012
1 parent 57d9848 commit 8c365c5
Show file tree
Hide file tree
Showing 7 changed files with 2,425 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

import android.content.Context;
Expand All @@ -23,6 +26,8 @@
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.assist.MemoryCacheKeyUtil;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
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.FakeBitmapDisplayer;

Expand Down Expand Up @@ -211,7 +216,7 @@ public void displayImage(String uri, ImageView imageView, DisplayImageOptions op
}
}

checkExecutors();
initExecutorsIfNeed();
ImageLoadingInfo imageLoadingInfo = new ImageLoadingInfo(uri, imageView, targetSize, options, listener, getLockForUri(uri));
LoadAndDisplayImageTask displayImageTask = new LoadAndDisplayImageTask(configuration, imageLoadingInfo, new Handler());
boolean isImageCachedOnDisc = configuration.discCache.get(uri).exists();
Expand Down Expand Up @@ -339,15 +344,22 @@ public void loadImage(Context context, String uri, ImageSize minImageSize, Displ
displayImage(uri, fakeImage, optionsWithFakeDisplayer, listener);
}

private void checkExecutors() {
private void initExecutorsIfNeed() {
if (imageLoadingExecutor == null || imageLoadingExecutor.isShutdown()) {
imageLoadingExecutor = Executors.newFixedThreadPool(configuration.threadPoolSize, configuration.displayImageThreadFactory);
imageLoadingExecutor = createExecutor();
}
if (cachedImageLoadingExecutor == null || cachedImageLoadingExecutor.isShutdown()) {
cachedImageLoadingExecutor = Executors.newFixedThreadPool(configuration.threadPoolSize, configuration.displayImageThreadFactory);
cachedImageLoadingExecutor = createExecutor();
}
}

private ExecutorService createExecutor() {
boolean lifo = configuration.tasksProcessingType == QueueProcessingType.LIFO;
BlockingQueue<Runnable> taskQueue = lifo ? new LIFOLinkedBlockingDeque<Runnable>() : new LinkedBlockingQueue<Runnable>();
return new ThreadPoolExecutor(configuration.threadPoolSize, configuration.threadPoolSize, 0L, TimeUnit.MILLISECONDS, taskQueue,
configuration.displayImageThreadFactory);
}

/** Returns memory cache */
public MemoryCacheAware<String, Bitmap> getMemoryCache() {
return configuration.memoryCache;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.nostra13.universalimageloader.cache.memory.MemoryCacheAware;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.download.ImageDownloader;

/**
Expand All @@ -35,14 +36,17 @@ public final class ImageLoaderConfiguration {
final int maxImageHeightForDiscCache;
final CompressFormat imageCompressFormatForDiscCache;
final int imageQualityForDiscCache;

final int threadPoolSize;
final boolean handleOutOfMemory;
final QueueProcessingType tasksProcessingType;

final MemoryCacheAware<String, Bitmap> memoryCache;
final DiscCacheAware discCache;
final ImageDownloader downloader;
final DisplayImageOptions defaultDisplayImageOptions;
final ThreadFactory displayImageThreadFactory;
final boolean loggingEnabled;
final ImageDownloader downloader;

private ImageLoaderConfiguration(final Builder builder) {
maxImageWidthForMemoryCache = builder.maxImageWidthForMemoryCache;
Expand All @@ -58,6 +62,7 @@ private ImageLoaderConfiguration(final Builder builder) {
defaultDisplayImageOptions = builder.defaultDisplayImageOptions;
loggingEnabled = builder.loggingEnabled;
downloader = builder.downloader;
tasksProcessingType = builder.tasksProcessingType;
displayImageThreadFactory = new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Expand Down Expand Up @@ -85,6 +90,7 @@ public Thread newThread(Runnable r) {
* <li>imageDownloader = {@link ImageDownloader#createDefault()}</li>
* <li>discCacheFileNameGenerator = {@link FileNameGenerator#createDefault()}</li>
* <li>defaultDisplayImageOptions = {@link DisplayImageOptions#createSimple() Simple options}</li>
* <li>tasksProcessingOrder = {@link QueueProcessingType#FIFO}</li>
* <li>detailed logging disabled</li>
* </ul>
* */
Expand Down Expand Up @@ -126,6 +132,7 @@ public static class Builder {
private int threadPriority = DEFAULT_THREAD_PRIORITY;
private boolean denyCacheImageMultipleSizesInMemory = false;
private boolean handleOutOfMemory = true;
private QueueProcessingType tasksProcessingType = QueueProcessingType.FIFO;

private int memoryCacheSize = DEFAULT_MEMORY_CACHE_SIZE;
private int discCacheSize = 0;
Expand Down Expand Up @@ -233,6 +240,15 @@ public Builder offOutOfMemoryHandling() {
return this;
}

/**
* Sets type of queue processing for tasks for loading and displaying images.<br />
* Default value - {@link QueueProcessingType#FIFO}
*/
public Builder tasksProcessingOrder(QueueProcessingType tasksProcessingType) {
this.tasksProcessingType = tasksProcessingType;
return this;
}

/**
* Sets maximum memory cache size for {@link android.graphics.Bitmap bitmaps} (in bytes).<br />
* Default value - {@link #DEFAULT_MEMORY_CACHE_SIZE this}<br />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.nostra13.universalimageloader.core.assist;

/**
* Queue processing type which will be used for display task processing
*
* @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
*/
public enum QueueProcessingType {
FIFO, LIFO
}
Loading

0 comments on commit 8c365c5

Please sign in to comment.