为什么WatchService使用未绑定通配符WatchEvent <?>而不是WatchEvent <Path>?

4

我刚刚按照这个教程来使用WatchService API,但是我不知道为什么要使用WatchEvent<?>而不是WatchEvent<Path>。如果我使用后者,则无需转换,或者是否有其他情况可以通过WatchService监视非路径事件

@SuppressWarnings("unchecked")
static <T> WatchEvent<T> cast(WatchEvent<?> event) {
    return (WatchEvent<T>)event;
}

void processEvents() {
    for (; ; ) {
        ...
        //why doesn't the poolEvents() return WatchEvent<Path> 
        for (WatchEvent<?> event: key.pollEvents()) {
            WatchEvent.Kind kind = event.kind();

            ...

            //here he use a static method cast() to SuppressWarnings the unchecked warning
            WatchEvent<Path> ev = cast(event);
        }
    }
}
1个回答

3

WatchService 的 Javadoc 说:

文件系统可能会比检索或处理事件更快地报告事件,并且实现可能对其可以累积的事件数量施加未指定的限制。如果实现知道丢弃事件,则它会安排键的 pollEvents 方法返回一个事件类型为 OVERFLOW 的元素。此事件可以被消费者用作重新检查对象状态的触发器。

StandardWatchEventKinds.OVERFLOW 的类型是 WatchEvent.Kind<Object>,这就是为什么 pollEvents 需要返回 WatchEvent<?> 列表而不是 WatchEvent<Path> 的原因。OVERFLOW 的 Javadoc 还提到:

此事件的上下文是特定于实现的,可能为 null。

这就是为什么溢出事件的类型需要是 WatchEvent<Object> 的原因。

请注意,您链接的教程建议执行以下操作:

使用kind方法检索事件类型。无论键已注册哪些事件,都可能接收到OVERFLOW事件。您可以选择处理溢出或忽略它,但应测试它。

因此,如果您尚未这样做,应将以下内容添加到您的代码中:
if (kind == OVERFLOW) {
    continue;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接