并发版本的Queue需要异常

发布于 2024-12-12 06:15:02 字数 385 浏览 0 评论 0原文

我正在编写一个实现队列接口的自定义队列。此实现是线程安全的,并且在某些情况下会阻塞。

普通的 Queue 接口不会提及异常,因此我无法在实现中抛出任何 InterruptedException

我看到了这个问题的两种解决方案,但它们都不是很令人满意:

  1. 删除队列接口并抛出异常。这使得代码无法用于需要队列的外国软件。

  2. 抛出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:

  1. Remove the Queue interface and throw Exceptions. This makes the code unusable for foreign software that requires a Queue.

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

翻了热茶 2024-12-19 06:15:02

如果您正在实现一个Queue,它可能需要抛出一个InterruptedException,那么我的猜测是您确实想实现一个BlockingQueue。如果您阅读此接口的 javadoc,您将意识到 Java 语言设计者基本上已经说过,在 BlockingQueue 的上下文中,正常的 Queue 操作要么成功,要么失败立即地。 Queue 接口允许在 add 方法无法成功时抛出 IllegalStateException,或者在 add 方法失败时返回 false。 >offer 方法失败。 引入了新方法 puttake 以及 offerpoll 的重载变体BlockingQueue 接口用于可能阻塞的操作,因此需要抛出 InterruptedException

If you are implementing a Queue which may need to throw an InterruptedException 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 a BlockingQueue the normal Queue operations will either succeed or fail immediately. The Queue interface provides for throwing an IllegalStateException if the add method can not succeed or returning false if the offer method fails. New methods put and take as well as an overloaded variant of offer and poll are introduced by the BlockingQueue interface for operations that may block and therefor need to throw an InterruptedException.

吖咩 2024-12-19 06:15:02

你应该选择选项一。

当人们想要拥有一个队列(接口)时,他们会期望队列以某种方式工作。行为在接口中指定,客户端无需担心实现或在切换时进行更改。

您的队列的情况并非如此,如果您实现此接口但抛出运行时异常,您将以意想不到的方式打破客户的期望。 外国软件要求使用队列正是因为他们不想被“欺骗”以获得不可互换的东西。

最好通过不实现队列来明确这一点,除非您可以处理中断的异常在您的对象内透明。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文