Extending interfaces and conversion errors in Java
我正在尝试实现" PriorityThreadPoolExecutor"。
因此,我宣布执行人:
1 2 | static ThreadPoolExecutor threadPool = new ThreadPoolExecutor(2, 100, 100, TimeUnit.SECONDS, queue); |
进行优先级排序的关键是队列参数。 基本上我需要一个PriorityBlockingQueue,所以我使用了构造函数:
1 2 3 4 5 6 7 8 9 | 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(); } }); |
mRunnable在哪里:
1 2 3 |
但是这里编译器抱怨:
1 2 | Type mismatch: cannot convert from PriorityBlockingQueue<mRunnable> to PriorityBlockingQueue <Runnable> |
我不明白为什么当mRunnable扩展Runnable时会发生此错误。
我可以修改比较器来修补它:
1 2 3 4 5 6 7 8 9 10 |
但这是一个丑陋的解决方案恕我直言。
您能解释一下为什么类型不匹配吗? 谢谢
由于不同的泛型类型而导致类型不匹配,因此您应该将引用类型从
或