I have a large number of images to retrieve from the server, and I want to get some photos with a higher priority than others, so I implemented my own ThreadPoolExecutor , which returns a FutureTask that implements Comparable , but it does not seem to work. Tasks are more or less processed in the order in which I add them to the queue. I was debugging the BlockingQueue my ThreadPoolExecutor and found out that when I add my Runnable with a higher priority, it does not move completely at the top of the queue. Here is the code
public class PriorityThreadPoolExecutor extends ThreadPoolExecutor { public PriorityThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) { super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue); } protected <T> RunnableFuture<T> newTaskForValue(Runnable runnable, T value) { return new ComparableFutureTask<T>(runnable, value); } protected class ComparableFutureTask<T> extends FutureTask<T> implements Comparable<ComparableFutureTask<T>> { private Object object; public ComparableFutureTask(Runnable runnable, T result) { super(runnable, result); object = runnable; } @Override @SuppressWarnings({ "unchecked", "rawtypes" }) public int compareTo(ComparableFutureTask<T> o) { if (this == o) { return 0; } if (o == null) { return -1;
And I add tasks to the pool as follows:
public BitmapLoader(Context context){ mThreadPoolExecutor = new PriorityThreadPoolExecutor(10, Integer.MAX_VALUE,//corepool and maxpool 1L, TimeUnit.SECONDS,//keep alive idle threads new PriorityBlockingQueue<Runnable>());//priority queue for jobs } public void queuePhoto(String url, ImageView imageView, int priority) { BitmapToLoad p = new BitmapToLoad(url, imageView, priority); final RunnableFuture<Object> futureTask = mThreadPoolExecutor.newTaskForValue(new BitmapLoaderRunnable(p), null); Log.d("BitmapLoader", "Scheduling job with priority " + priority); mThreadPoolExecutor.execute(futureTask); }
My BitmapLoaderRunnable implements Comparable and, when debugging, the compareTo method is called. What am I doing wrong? Thanks
EDIT: below is the code of my runnables
private class BitmapLoaderRunnable implements Runnable, Comparable<BitmapLoaderRunnable> { private BitmapToLoad bitmapToLoad; public BitmapLoaderRunnable(BitmapToLoad bitmap) { this.bitmapToLoad = bitmap; } @Override public void run() { try{ if(imageViewReused(bitmapToLoad)) return; Thread.sleep(1000); Bitmap bmp = getBitmap(bitmapToLoad.url); BitmapCache.put(bitmapToLoad.url, bmp); if(imageViewReused(bitmapToLoad)) return; BitmapDisplayer bd = new BitmapDisplayer(bmp, bitmapToLoad); mHandler.post(bd); } catch(Throwable th){ th.printStackTrace(); } } @Override public int compareTo(BitmapLoaderRunnable other) { return this.bitmapToLoad.priority - other.bitmapToLoad.priority; } }