WatchService在不同线程中的不同实例

时间:2015-12-18 13:39:18

标签: java multithreading thread-safety java-7 watchservice

我需要在下面的实现中使用watchService。这在A类中是存在的。并且多个线程创建该线程的不同实例并使用它。我遇到了这个代码的多个问题。首先,很多事件都会丢失,我可以在不同的线程中看到它们但不是所有线程中的所有事件,每个随机线程只有一个事件。我需要在所有线程中都存在所有事件。请帮我解决这个实现的错误。

WatchService不能在多个线程中使用吗?据我所知,它是线程安全的。另外,我知道这与keys.reset()和查找文件的return true方法有关。

请帮帮我。提前致谢

   public boolean watchDir(final String fileName) throws Exception {

    WatchKey key =null;
    long startTime = System.currentTimeMillis();
    logger.debug("Looking for file : " + fileName);
    try {
        while ((System.currentTimeMillis() - startTime) / (1000) <= watchServiceTimeoutinSecs) {

            // wait for key to be signalled

            key = watcher.take();
            Path dir = keys.get(key);
            //logger.debug(" Waiting here for file: "+ fileName );
            if (dir == null) {
                continue;
            }

            for (WatchEvent<?> event : key.pollEvents()) {
                WatchEvent.Kind kind = event.kind();

                // TBD - provide example of how OVERFLOW event is handled
                if (kind == OVERFLOW) {
                    continue;
                }

                // Context for directory entry event is the file name of entry
                WatchEvent<Path> ev = cast(event);
                Path name = ev.context();
                Path child = dir.resolve(name);

                logger.debug("just seen file " + child.getFileName() + " Waiting here for file: " + fileName + event.kind().name());
                // print out event
                if (isWatchFile) {
                    if (child.getFileName().toString().equalsIgnoreCase(fileName)&& eventType.equalsIgnoreCase(event.kind().name())) {
                        logger.debug("Found file : " + fileName);
                        //watcher.close();
                        //return true;
                    }
                } else {
                    logger.debug("Event type: " + event.kind().name() + " File Name : " + child.getFileName());
                }

                // if directory is created, and watching recursively, then
                // register it and its sub-directories
                if (recursive && (kind == ENTRY_CREATE)) {
                    try {
                        if (Files.isDirectory(child, NOFOLLOW_LINKS)) {
                            registerAll(child);
                        }
                    } catch (IOException x) {
                        // ignore to keep sample readable
                    }
                }

            }
            if (!key.reset()) {
            keys.remove(key);

            // all directories are inaccessible
            if (keys.isEmpty()) {
            }
        }
            //Wait in between consecutive polling

            Thread.sleep(watchServiceFrequencyInMillis);
        }

        //Return false if time exceeds that ofthe watchServicetimeoutinMillis
        logger.debug("Time Exceeded looking for file : " + fileName);
        return false;
    } finally {
        // reset key and remove from set if directory no longer accessible
        if (!key.reset()) {
            keys.remove(key);

            // all directories are inaccessible
            if (keys.isEmpty()) {
            }
        }
    }
} // watchDir

0 个答案:

没有答案