MDB 中的 JDK 5 文件/流读取超时 API

发布于 2024-10-09 18:47:22 字数 308 浏览 0 评论 0原文

是否有 JDK 5 或更早版本的 API 用于读取文件和/或读取具有超时的流?此外,该 API 在封装在 MDB 中时必须是安全的。仅供参考:我已经研究过使用 FutureResult、TimedCallable 类等;我得出的结论是,在 MDB 中使用这些是不可取的,因为 1. 这些不是 JDK 本机类,2. 线程是在 MDB 内部生成的;所以我需要另一个解决方案。

另外,我认为任何解决方案都需要线程,因为必须有一个线程进行读取,并且有一个线程管理读取线程的及时性并在必要时阻止它。那么,这种行为在MDB内部是否违法呢?如果所述 API 是 JDK 原生的,那么它的实现会安全吗?

Is there a JDK 5 or older API for reading files and or reading streams with a timeout? Also, this API must be safe when wrapped inside an MDB. FYI: I have already looked into using the FutureResult, TimedCallable classes and the like; I have come to the conclusion that using these within in MDB is not advisable since 1. these are not JDK native classes, and 2. threads are spawned inside the MDB; so I need another solution.

Also I would believe that any solution would require threading since there must be a thread that reads and one that manages the the timeliness of the read thread and blocks it if necessary. Therefore, would such a behavior be illegal inside an MDB? What if said API is native to the JDK will this make it safe to implement?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

给我一枪 2024-10-16 18:47:22

正如您从 InputStream API 中看到的那样,对此没有通用的约定。然而,某些流(例如从套接字获取的输入流)具有超时行为,但这必须在套接字上配置。

AFAIK,您可以尝试通过调用 Interrupt() 来手动中断读取线程,并希望 Stream 的底层实现不会捕获它而不重新抛出它(在监视器上使用 wait() 时的常见错误)。然而,这必须在额外的线程中完成,或者通过使用上面提到的 Future API 来完成。如果您捕获此 InterruptedException 并适当处理它,您应该有一个相当稳定的解决方案。

PS:什么是MDB?我只知道 Microsoft Access 使用的 Microsoft 数据库格式的缩写。

As you see from the InputStream API there is no general contract for this. However some Streams, like the InputStream you get from a socket, have a timeout behaviour, but this has to be configured on the socket.

AFAIK you can try to interrupt the reading Thread manually, by calling interrupt() on it, and hope that the underlying implementation of your Stream does not catch this without rethrowing it (A common mistake when using wait() on a monitor). This however has to be done in an extra thread, or by using the Future API you mentioned above. If you catch this InterruptedException, and handle it appropriately, you should have a fairly stable solution.

PS: What is an MDB? I only know this acronym for the Microsoft Database format used by Microsoft Access.

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