android - AsyncTask on Executor and PriorityBlockingQueue implementation issue -


on wave of this question , many hints another one, i'm trying implement asynctask variant tasks can prioritized.

in customasynctask class have:

public abstract class customasynctask<params, progress, result> {      private static int core_pool_size = 1;     private static int maximum_pool_size = 1;      private static final int keep_alive = 1;      private static final threadfactory sthreadfactory = new threadfactory() {         private final atomicinteger mcount = new atomicinteger(1);          public thread newthread(runnable r) {             return new thread(r, "customasynctask #" + mcount.getandincrement());         }     };      private static final blockingqueue<downloadtask> ppoolworkqueue =             new priorityblockingqueue<downloadtask>(10, new downloadtaskscomparator());      @suppresswarnings({ "unchecked", "rawtypes" })     public static executor priority_thread_pool_executor             = new threadpoolexecutor(core_pool_size, maximum_pool_size, keep_alive,                     timeunit.seconds, (priorityblockingqueue) ppoolworkqueue, sthreadfactory);     //... } 

the comparator:

public class downloadtaskscomparator implements comparator<downloadtask> {      @override     public int compare(downloadtask arg0, downloadtask arg1) {         int res;          if (arg0 == null && arg1 == null) {             res = 0;         } else if (arg0 == null) {             res = -1;         } else if (arg1 == null) {             res = 1;         }          res = arg0.getpriority() - arg1.getpriority();          return res;     } } 

in downloadtask class extending customasynctask have priority integer field , getpriority() method.

i'm calling tasks execution as:

downloadtask dt = new downloadtask(..., priority_normal, ...); dt.executeonexecutor(customasynctask.priority_thread_pool_executor); 

this works: if pool sizes 1, downloads executed 1 one; if pool size 2, etc.

note: priority integers have arbitrary values:

public static final int priority_high = 10; public static final int priority_normal = 1; 

but if call tasks as:

downloadtask dt = new downloadtask(..., priority_high, ...); dt.executeonexecutor(customasynctask.priority_thread_pool_executor); 

i have java.lang.classcastexception: my.pkg.name.customasynctask$3 cannot cast my.pkg.name.downloadtask

and then

at my.pkg.name.downloadtaskscomparator.compare(downloadtaskscomparator.java:1) @ java.util.concurrent.priorityblockingqueue.siftupusingcomparator(priorityblockingqueue.java:334) @ java.util.concurrent.priorityblockingqueue.offer(priorityblockingqueue.java:447) @ java.util.concurrent.threadpoolexecutor.execute(threadpoolexecutor.java:1295) @ my.pkg.name.customasynctask.executeonexecutor(customasynctask.java:494) @ my.pkg.name.getdownloadtasklistener$1.finishdownload(getdownloadtasklistener.java:180) @ my.pkg.name.downloadtask.onpostexecute(downloadtask.java:330) @ my.pkg.name.downloadtask.onpostexecute(downloadtask.java:1) @ my.pkg.name.customasynctask.finish(customasynctask.java:536) @ my.pkg.name.customasynctask.access$0(customasynctask.java:532) @ my.pkg.name.customasynctask$internalhandler.handlemessage(customasynctask.java:549) @ android.os.handler.dispatchmessage(handler.java:99) @ android.os.looper.loop(looper.java:137) @ android.app.activitythread.main(activitythread.java:4745) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:786) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) @ dalvik.system.nativestart.main(native method) 

all androidruntime

i don't have clue...

edit: @ point, i've wrapped small eclipse project implements things same way of bigger application , suffers same issue. borrows customasynctaskcomparator , customasynctask verbatim. no visual feedback given. app's progress logcat output. gives idea. when more 2 tasks enqueued, app fcs.

https://www.dropbox.com/s/lrg4kscgw3f1xwr/concurrenttest.tar.gz

as may have noticed while looking through asynctask implementation, internally uses futuretask handle background task, , gets handed on executor , potentially queued on it's work queue. since deriving own implementation, replace futuretask custom derivative holds reference asynctask, accessed comparator implementation.

also, instead of replacing default static executor of custom asynctask derivative, should instead use executeonexecutor() method in subclasses can used in generic manner.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -