我有以下代码的变体:
package com.test.package;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class TestClass {
public static class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("Called");
}
}
public void method() {
PriorityBlockingQueue<MyRunnable> queue = new PriorityBlockingQueue<MyRunnable>();
method2(queue);
}
public void method2(BlockingQueue<? extends Runnable> queue) {
System.out.println(queue);
// Getting error here because BlockingQueue<? extends Runnable> is not a
// subtype of BlockingQueue<Runnable>.
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(200, 200, 0L,
TimeUnit.MILLISECONDS, queue);
}
}
如您所见,我的队列与
ThreadPoolExecutor
构造函数不兼容。除了将队列转换到(BlockingQueue<Runnable>)
之外,还有什么方法可以解决此问题?我显然无法修补Java标准库。 最佳答案
不,你不应该。
当然,您的BlockingQueue<MyRunnable>
应该只包含MyRunnable
。但是ThreadPoolExecutor
可以将任意Runnable
任务提交到您给它的队列中:请参阅 execute(Runnable command)
。
如果发生这种情况,您的队列中可能有一个非MyRunnable
实例。然后,您尝试从该队列的引用中轮询(键入为BlockingQueue<MyRunnable>
),并获得ClassCastException
。
简单的例子:
PriorityBlockingQueue<MyRunnable> queue = new PriorityBlockingQueue<>();
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(200, 200, 0L,
TimeUnit.MILLISECONDS, queue);
threadPool.execute(new WhateverRunnable());
MyRunnable myRunnable = queue.poll(); // this could throw ClassCastException
如果
queue.poll()
发生在线程池有机会使WhateverRunnable
实例出队之前,则上面的代码将引发异常。关于java - 如何使我的泛型代码与此方法签名兼容?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24636199/