@Override
        public void run() {
            WatchKey watchKey;
            infiniteLoop:
            while (!Thread.currentThread().isInterrupted() && watching) {

                try {
                    watchKey = watchService.take();
                } catch (InterruptedException e) {
                    break infiniteLoop;
                }

                for (WatchEvent<?> event : watchKey.pollEvents()) {
                    if (Thread.currentThread().isInterrupted()) {
                        break infiniteLoop;
                    }
                    WatchEvent<Path> pathEvent = silentlyCast(event);
                    Path absoluteEventPath = directory.resolve(pathEvent.context());
                    handler.onEvent(pathEvent.kind(), absoluteEventPath);
                }

                watchKey.reset();
            }

            try {
                // We need to close the WatchService here and not in the stopWatching() method, just in case
                // the Handler itself interrupts this thread.
                watchService.close();
            } catch (IOException ignored) {
            }

        }