我有一个类型,MyType<T>
包含泛型类型 T
。我有一个类型为 BlockingQueue<MyType<T>>
的阻塞队列。我想将“流结束”标记发送到队列,即毒丸,但问题是由于通用类型,我无法实例化毒丸。有办法解决这个问题吗?
最佳答案
您应该能够创建一个。
class MyType<T> {
private BlockingQueue<MyType<T>> q = new ArrayBlockingQueue<>(10);
// Poison pill to signal the end of the queue.
public static final MyType<?> PILL = new MyType<>();
/**
* Special private constructor for PILL creation.
*/
private MyType() {
}
public boolean queueClosed() {
return q.peek() == PILL;
}
}
如果您有自己的构造函数,则可以添加不带参数的私有(private)
构造函数。
关于java - 当队列类型包含泛型时,对阻塞队列进行毒害,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31384529/