Java 中的优先级 ThreadPoolExecutor (Android)
我正在尝试创建一个具有优先级的 ThreadPoolExecutor 。所以我定义了一个
private static ThreadPoolExecutor threadpool = new ThreadPoolExecutor(30, MAXPOOL, MAXPOOL, TimeUnit.SECONDS,
queue, new mThreadFactory());
所以现在关键是队列引用。但是当我声明时:
static PriorityBlockingQueue<mDownloadThread> queue=new PriorityBlockingQueue<mDownloadThread>(MAXPOOL,new DownloadThreadComparator());
编译器在第一行给出错误:构造函数 ThreadPoolExecutor(int, int, int, TimeUnit, PriorityBlockingQueue, FileAccess.mThreadFactory) 未定义,并使用一个快速修复:将“队列”类型更改为 BlockingQueue。你能帮我了解问题所在吗?
谢谢
附加信息:
为了比较可运行对象,我实现了以下类
class mDownloadThread implements Runnable{
private Runnable mRunnable;
private int priority;
mDownloadThread(Runnable runnable){
mRunnable=runnable;
}
@Override
public void run() {
mRunnable.run();
}
public int getPriority() {
return priority;
}
public void setPriority(int priority) {
this.priority = priority;
}
}
比较器:
class DownloadThreadComparator implements Comparator<mDownloadThread>{
@Override
public int compare(mDownloadThread arg0, mDownloadThread arg1) {
if (arg0==null && arg1==null){
return 0;
} else if (arg0==null){
return -1;
} else if (arg1==null){
return 1;
}
return arg0.getPriority()-arg1.getPriority();
}
}
I'm trying to make an ThreadPoolExecutor with priority. So I define a
private static ThreadPoolExecutor threadpool = new ThreadPoolExecutor(30, MAXPOOL, MAXPOOL, TimeUnit.SECONDS,
queue, new mThreadFactory());
So the key is the queue reference now. But when I declare :
static PriorityBlockingQueue<mDownloadThread> queue=new PriorityBlockingQueue<mDownloadThread>(MAXPOOL,new DownloadThreadComparator());
The compiler gives an error in the first line: The constructor ThreadPoolExecutor(int, int, int, TimeUnit, PriorityBlockingQueue, FileAccess.mThreadFactory) is undefined with a single quickfix: Change type of 'queue' to BlockingQueue. Can you help me to understand whats the problem?
Thanks
Additional Info:
For comparing the runnables I implementd the following class
class mDownloadThread implements Runnable{
private Runnable mRunnable;
private int priority;
mDownloadThread(Runnable runnable){
mRunnable=runnable;
}
@Override
public void run() {
mRunnable.run();
}
public int getPriority() {
return priority;
}
public void setPriority(int priority) {
this.priority = priority;
}
}
The comparator:
class DownloadThreadComparator implements Comparator<mDownloadThread>{
@Override
public int compare(mDownloadThread arg0, mDownloadThread arg1) {
if (arg0==null && arg1==null){
return 0;
} else if (arg0==null){
return -1;
} else if (arg1==null){
return 1;
}
return arg0.getPriority()-arg1.getPriority();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ThreadPoolExecutor
构造函数接受BlockingQueue
而不是BlockingQueue
,因此您无法将其传递给PriorityBlockingQueue
实例。您可以将
queue
类型更改为PriorityBlockingQueue
,但在这种情况下,您将无法实现Comparator
在compare
方法内进行转换。另一种解决方案是绕过通用类型检查,但您有责任仅将
mDownloadThread
实例提交给execute
方法:ThreadPoolExecutor
constructor acceptsBlockingQueue<Runnable>
and notBlockingQueue<? extends Runnable>
, thus you can't pass to itPriorityBlockingQueue<mDownloadThread>
instance.You can change type of
queue
toPriorityBlockingQueue<Runnable>
, but in that case you won't be able to implementComparator<mDownloadThread>
without casting inside thecompare
method.The other solution is to bypass generic type-checking but it would be your responsibility to submit only instances of
mDownloadThread
toexecute
method:问题是
ThreadPoolExecutor
需要一个BlockingQueue
而您正在传递一个PriorityBlockingQueue
。PriorityBlockingQueue
实现了BlockingQueue
,因此这不是问题。问题在于Runnable
。您无法在需要Runnable
的地方传递通用类型的mDownloadThread
。修复如下:
&&
如果编写
ThreadPoolExecutor
来接受BlockingQueue<?扩展 Runnable>
你所拥有的可以工作。The problem is that
ThreadPoolExecutor
is expecting aBlockingQueue<Runnable>
and you are passing aPriorityBlockingQueue<mDownloadThread>
. ThePriorityBlockingQueue
implementsBlockingQueue
so that is not the problem. The problem is theRunnable
. You cannot pass a generic type ofmDownloadThread
whereRunnable
was expected.Fix is the following:
&&
If
ThreadPoolExecutor
were written to accept aBlockingQueue<? extends Runnable>
what you have would work.