我搜索了很多,但找不到解决我的问题的方法。
我有自己的班级,BaseTask
使用 aThreadPoolExecutor
来处理任务。我想要任务优先级,但是当我尝试使用 a 时,PriorityBlockingQueue
我得到ClassCastException
了因为ThreadPoolExecutor
它将我的任务包装到一个FutureTask
对象中。
这显然是有道理的,因为FutureTask
没有实现Comparable
,但是我将如何继续解决优先级问题?我读过你可以覆盖newTaskFor()
in ThreadPoolExecutor
,但我似乎根本找不到这种方法......?
我们欢迎所有的建议!
一些帮助的代码:
在我的BaseTask
课上,我有
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);
}
在BaseFutureTask
课堂上
@Override
public int compareTo(BaseFutureTask another) {
long diff = this.priority - another.priority;
return Long.signum(diff);
}
在BaseThreadPoolExecutor
类中,我覆盖了 3 个submit
方法......这个类中的构造函数被调用,但没有一个submit
方法