我有大量图片要从服务器获取,并且我想获取比其他图片更高优先级的图片,因此我实现了自己的ThreadPoolExecutor,该返回的FutureTask实现了Comparable,但似乎不起作用。任务按照我将它们添加到队列的顺序进行或多或少的处理。我调试了BlockingQueueThreadPoolExecutor,发现当我以更高的优先级添加Runnable时,它并没有一直移到队列的顶部。这是代码

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;
        }
    }

最佳答案

PriorityQueue的头部是最小的元素。因此,如果您想首先获得最高优先级,则需要撤消比较。

    @Override
    public int compareTo(BitmapLoaderRunnable other) {
        return other.bitmapToLoad.priority - this.bitmapToLoad.priority;
    }

07-27 21:07