并发版本的Queue需要异常
我正在编写一个实现队列接口的自定义队列。此实现是线程安全的,并且在某些情况下会阻塞。
普通的 Queue 接口不会提及异常,因此我无法在实现中抛出任何 InterruptedException
。
我看到了这个问题的两种解决方案,但它们都不是很令人满意:
删除队列接口并抛出异常。这使得代码无法用于需要队列的外国软件。
抛出
RuntimeException
,这将产生大量非常令人惊讶的软件活动,我不想冒险。
不知何故,诸如 ArrayBlockingQueue 之类的实现设法实现了 Queue 和 BlockingQueue。这是要走的路吗?或者这里有什么技巧?
I am writing a custom queue implementing the queue interface. This implementation is thread safe and blocks on certain occasions.
The normal Queue interface does not mention Exceptions, therefore I can't throw any InterruptedException
in my implementation.
I see two solutions for this problem, but they are both not very satisfying:
Remove the Queue interface and throw Exceptions. This makes the code unusable for foreign software that requires a Queue.
Throw
RuntimeException
, this will yield tonnes of very surprising software activity, which I don't want to risk.
Somehow implementations like ArrayBlockingQueue
manage to implement Queue
and BlockingQueue
. Is that the way to go, or what is the trick here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您正在实现一个
Queue
,它可能需要抛出一个InterruptedException
,那么我的猜测是您确实想实现一个BlockingQueue。如果您阅读此接口的 javadoc,您将意识到 Java 语言设计者基本上已经说过,在BlockingQueue
的上下文中,正常的Queue
操作要么成功,要么失败立即地。Queue
接口允许在add
方法无法成功时抛出IllegalStateException
,或者在add
方法失败时返回false
。 >offer 方法失败。引入了新方法
接口用于可能阻塞的操作,因此需要抛出put
和take
以及offer
和poll
的重载变体BlockingQueueInterruptedException
。If you are implementing a
Queue
which may need to throw anInterruptedException
then my guess is that you really want to implement a BlockingQueue. If you read the javadoc for this interface, you will realize that the Java language designers have basically said that in the context of aBlockingQueue
the normalQueue
operations will either succeed or fail immediately. TheQueue
interface provides for throwing anIllegalStateException
if theadd
method can not succeed or returningfalse
if theoffer
method fails. New methodsput
andtake
as well as an overloaded variant ofoffer
andpoll
are introduced by theBlockingQueue
interface for operations that may block and therefor need to throw anInterruptedException
.你应该选择选项一。
当人们想要拥有一个队列(接口)时,他们会期望队列以某种方式工作。行为在接口中指定,客户端无需担心实现或在切换时进行更改。
您的队列的情况并非如此,如果您实现此接口但抛出运行时异常,您将以意想不到的方式打破客户的期望。 外国软件要求使用队列正是因为他们不想被“欺骗”以获得不可互换的东西。
最好通过不实现队列来明确这一点,除非您可以处理中断的异常在您的对象内透明。
You should go with option one.
When people want to have a Queue (interface) on their hand they will expect the queue to work a certain way. The behavior is specified in the interface and the client do not need to worry about the implementation or make changes when they switch.
This is not the case with your queue, if you implement this interface but throws run-time exception you will be breaking the expectation of your client in an unexpected manner. The foreign software asks for a Queue precisely because they don't want to be "tricked" to get something non-interchangeable.
Better to make this explicit by not implementing Queue, unless you can handle the interrupted exception transparently within your object.