由PriorityBlockingQueue支持的ThreadPoolExecutor似乎不起作用

cho*_*hop 6 java priority-queue threadpool threadpoolexecutor

我有很多图片要从服务器上获取,我想要获取一些优先级高于其他图片的图片,所以我已经实现了我自己的图片ThreadPoolExecutor返回一个FutureTask实现,Comparable但它似乎不起作用.这些任务或多或少按照我将它们添加到队列的顺序进行处理.我调试了BlockingQueue我的ThreadPoolExecutor并发现当我添加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;
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

我以这种方式将任务添加到池中:

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);
}
Run Code Online (Sandbox Code Playgroud)

正在调用我的BitmapLoaderRunnable工具Comparable和调试compareTo方法.我究竟做错了什么?谢谢

编辑:下面是我的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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

jta*_*orn 8

a的头PriorityQueue最少元素.因此,如果您想先获得最高优先级,则需要撤消比较.

    @Override
    public int compareTo(BitmapLoaderRunnable other) {
        return other.bitmapToLoad.priority - this.bitmapToLoad.priority;
    }
Run Code Online (Sandbox Code Playgroud)

  • 天啊,昨天花了整整一天试图解决它,grrrrrrrrrrrrrr.这将教我从现在开始正确阅读文档.非常感谢! (3认同)