I'm trying to implement a "PriorityThreadPoolExecutor".
So I declare the Executor:
static ThreadPoolExecutor threadPool =
new ThreadPoolExecutor(2, 100, 100, TimeUnit.SECONDS, queue);
And the key for the prioritization is the queue paramether. Basically I need a PriorityBlockingQueue so I use the constructor:
PriorityBlockingQueue(int initialCapacity, Comparator<? super E> comparator);
queue = new PriorityBlockingQueue<mRunnable>(10,
new Comparator<mRunnable>() {
@Override
public int compare(mRunnable lhs, mRunnable rhs) {
return rhs.getPriority()-lhs.getPriority();
}
});
Where the mRunnable is:
public interface mRunnable extends Runnable {
public int getPriority();
}
But here the compiler complains:
Type mismatch: cannot convert from PriorityBlockingQueue<mRunnable>
to PriorityBlockingQueue <Runnable>
I do not understand why is this error happening when mRunnable extends Runnable.
I could patch it changing the comparator:
new Comparator<Runnable>() {
@Override
public int compare(Runnable lhs, Runnable rhs) {
if (lhs instanceof mRunnable && rhs instanceof mRunnable){
return ((mRunnable)rhs).getPriority()-((mRunnable)lhs).getPriority();
}
return 0;
}
});
But this is a ugly sollution imho.
Can you explain why is the Type mismatch is happening? Thanks