本文介绍了释放Java 7 WatchService的资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java 7 WatchService来查看目录。我不断改变我正在观看的目录。我遇到异常:

I am using Java 7 WatchService to watch directories. I constantly change which directories i am watching. I run into the exception:

java.io.IOException:已达到网络BIOS命令限制。

java.io.IOException: The network BIOS command limit has been reached.

50个目录后。我确定我在创建一个新的WatchService之前调用close()。

after 50 directories. I am certain i call close() on each WatchService i create before creating a new one.

有没有人知道发布WatchService的正确方法,所以你不要遇到这个限额?

Does anyone know the proper way to release a WatchService so you do not run into this limit?

谢谢,

戴夫

推荐答案

我认为所有你需要做的是 close()该服务。我知道你说你认为你已经这样做了,但我怀疑你错过了一些。例如,在异常的情况下,您可能无法关闭服务实例。您应该将WatchService实例视为其他IO资源,并在finally块中将其关闭;例如

I think that all you need to do is close() the service. I know you said that you think that you do this already, but I suspect that you are missing some. For instance, you could be failing to close service instances in the case of an exception. You should treat a WatchService instance as other IO resources and close it in a finally block; e.g.

WatchService ws = ...
try {
    // use it ...
} finally {
    ws.close();
}

或使用Java 7try with resource语法。

or using the Java 7 "try with resource" syntax.

try (WatchService ws = ...) {
    // use it ...
}

WatchService 关闭时,它应立即释放任何O /它持有的/ S级资源。

When the WatchService is closed, it should immediately free any O/S level resources that it holds.

唯一的另一种可能性是你遇到了一些Java漏洞code> WatchService 实施。

The only other possibility is that you have run into some Java bug in the WatchService implementation.

这篇关于释放Java 7 WatchService的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 18:21