WatchService:在注册时传递参数

WatchService: passing parameter at registration

我正在寻找一种在将路径注册到 WatchService 时传递参数的方法。目标是在处理与路径相关的事件时获取这些参数。

WatchService watchService = FileSystems.getDefault().newWatchService();
....
path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE); //I would like to pass some parameters here...
....
key = watchService.take(); // ... so that I can get these parameters here from the WatchKey

知道是否可行吗?

谢谢, 迈克尔

刚发现注册时返回的WatchKeytake()/poll()方法返回的是一样的

我设法通过维护一个外部 Map<WatchKey, ....>.

来解决它