I have a large number of pictures to fetch from a server, and I want to fetch some pictures with higher priority than others so I've implemented my own ThreadPoolExecutor
that returns a FutureTask
that implements Comparable
but it doesn't seem to work. The tasks are more or less processed in the order I add them to the queue. I've debugged the BlockingQueue
of my ThreadPoolExecutor
and found out that when I add my Runnable
with a higher priority, it is not shifted all the way up 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; // this has higher priority than null
}
if (object != null && o.object != null) {
if (object.getClass().equals(o.object.getClass())) {
if (object instanceof Comparable) {
return ((Comparable) object).compareTo(o.object);
}
}
}
return 0;
}
}
}
And I add the tasks to the pool in this way:
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 I debug the compareTo
method is being 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;
}
}
The head of a PriorityQueue
is the least element. so if you want the highest priority first, you need to reverse your comparison.
@Override
public int compareTo(BitmapLoaderRunnable other) {
return other.bitmapToLoad.priority - this.bitmapToLoad.priority;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With