I've searched a lot but could not find a solutuion to my problem.
I have my own class, BaseTask
, that uses a ThreadPoolExecutor
to handle tasks.
If I don't want prioritization (i.e. using a LinkedBlockingQueue
) this works just fine, but when I try to use a PriorityBlockingQueue
I get ClassCastException
because the ThreadPoolExecutor
wraps my Tasks into a FutureTask
object.
This is obviously OK because the FutureTask
does not implement Comparable
, but how would I go on to solve the priority problem?
I've read that you could override newTaskFor
in ThreadPoolExecutor
, but I can not seem to find this method at all...?
Any suggestions would be much appreciated!
Some code to help:
In my BaseTask
class I have
private static final BlockingQueue<Runnable> sWorkQueue = new PriorityBlockingQueue<Runnable>();
private static final ThreadFactory sThreadFactory = new ThreadFactory() {
private final AtomicInteger mCount = new AtomicInteger(1);
public Thread newThread(Runnable r) {
return new Thread(r, "AsyncTask #" + mCount.getAndIncrement());
}
};
private static final BaseThreadPoolExecutor sExecutor = new BaseThreadPoolExecutor(
1, Integer.MAX_VALUE, 10, TimeUnit.SECONDS, sWorkQueue, sThreadFactory);
private final BaseFutureTask<Result> mFuture;
public BaseTask(int priority) {
mFuture = new BaseFutureTask<Result>(mWorker, priority);
}
public final BaseTask<Params, Progress, Result> execute(Params... params) {
/* Some unimportant code here */
sExecutor.execute(mFuture);
}
In BaseFutureTask
class
@Override
public int compareTo(BaseFutureTask another) {
long diff = this.priority - another.priority;
return Long.signum(diff);
}
In BaseThreadPoolExecutor
class i override the 3 submit
methods...
The constructor in this class gets called, but none of the submit
methods