gpt4 book ai didi

java - 更新USB设备列表

转载 作者:搜寻专家 更新时间:2023-11-01 02:30:47 24 4
gpt4 key购买 nike

我想创建自动更新 USB 设备列表的模块(不仅是大容量存储)。现在它工作起来非常粗鲁——它有额外的线程,在 1 秒后更新设备列表。但也许您知道如何处理一些插件事件,这些事件会生成操作系统(例如 Windows XP 及更高版本)或如何简化此过程的其他方式。感谢您的宝贵时间!

最佳答案

这个问题似乎一半是设计,一半是实现。看来您最关心的是为 jUSB 之类的 Java USB 服务寻找某种方式从系统服务热插拔事件,允许您通过 inversion of control 调用事件而不是运行一个线程,在后台热情地、迭代地轮询系统。

您寻求的支持was implemented back in 2005 as JSR-80,并且似乎继续存在,在所有超出其商业支持的伟大项目中,javax-usb on SourceForge .请注意,在他们的玩具示例中(在 javax-usb-example/src/ShowTopology.java 中),他们的首选模型仍然基于轮询。

然而,并不是所有的东西都丢失了!在 the properties file for javax.usb ,我们发现:

# If polling should be used instead of waiting for a hotplug notification.

# Some kernel versions have a bug, and don't update the device node(s) until many seconds after waking up threads waiting on the devices file. If you are not getting hotplug events on a moderately to heavily loaded system, use polling.

# Note that since the new (buggy?) kernel behavior breaks javax.usb, this defaults to true!

# If you know your kernel isn't broken you can set this to false.

#com.ibm.jusb.os.linux.LinuxUsbServices.topologyUpdateUsePolling = true

并且在 javax-usb/src/javax/usb/event/UsbServicesListener.java ,我们看到了一个用于设置 USB 事件监听器的模型,您可以在设备连接到系统或从系统断开时继续使用它。然而,没有关于此是否有效的外部文档,而且文档本身似乎已有 8 年历史。

因此,可以合理地假设虽然支持理论上 存在,但它是滞后的、陈旧的,并且迫切需要社区进行更新。这是 SO 上的这个优秀社区可以帮助解决的问题,所以我将在 pointing directly to the project itself. 之前结束。

目前,在增强的硬件或热插拔支持出现之前,轮询仍然是最好的解决方案。

( Additional reference implementation of javax-usb )

关于java - 更新USB设备列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9932206/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com