java - ThreadPoolExecutor backed by PriorityBlockingQueue doesn't seem to work -
i have large number of pictures fetch server, , want fetch pictures higher priority others i've implemented own threadpoolexecutor
returns futuretask
implements comparable
doesn't seem work. tasks more or less processed in order add them queue. i've debugged blockingqueue
of threadpoolexecutor
, found out when add runnable
higher priority, not shifted way @ top of queue. here 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; // has higher priority 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 add tasks pool in way:
public bitmaploader(context context){ mthreadpoolexecutor = new prioritythreadpoolexecutor(10, integer.max_value,//corepool , maxpool 1l, timeunit.seconds,//keep alive idle threads new priorityblockingqueue<runnable>());//priority queue 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 priority " + priority); mthreadpoolexecutor.execute(futuretask); }
my bitmaploaderrunnable
implements comparable
, when debug compareto
method being called. doing wrong? thanks
edit: below code of 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 priorityqueue
least element. if want highest priority first, need reverse comparison.
@override public int compareto(bitmaploaderrunnable other) { return other.bitmaptoload.priority - this.bitmaptoload.priority; }