我有大量图片要从服务器获取,我想获取一些优先级高于其他图片的图片,所以我实现了自己的图片ThreadPoolExecutor
,它返回一个FutureTask
实现Comparable
但它似乎不起作用的图片。这些任务或多或少按照我将它们添加到队列中的顺序进行处理。我已经调试了BlockingQueue
myThreadPoolExecutor
并发现当我添加Runnable
更高优先级的 my 时,它并没有在队列顶部一直向上移动。这是代码
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;
}
}
}
我以这种方式将任务添加到池中:
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);
}
我的BitmapLoaderRunnable
工具Comparable
和当我调试该compareTo
方法时被调用。我究竟做错了什么?谢谢
编辑:下面是我的可运行文件的代码
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;
}
}