Skip to content

Commit

Permalink
perf(Concurrency): improve Java object lock for FileWatcher::terminate()
Browse files Browse the repository at this point in the history
improve Java object lock for FileWatcher::terminate()

BREAKING CHANGE: improve Java object lock for FileWatcher::terminate()
  • Loading branch information
johnnymillergh committed Oct 27, 2020
1 parent f1b9720 commit fe41844
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ public void run(String... args) {
@PreDestroy
private void preDestroy() {
log.debug("Destroying {}, fileWatcher: {}", this.getClass().getSimpleName(), fileWatcher);
Optional.ofNullable(fileWatcher).ifPresent(FileWatcher::destroy);
Optional.ofNullable(fileWatcher).ifPresent(fileWatcher1 -> {
fileWatcher1.terminate();
log.debug("FileWatcher terminated: {}", fileWatcher1.isTerminated());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.sun.nio.file.SensitivityWatchEventModifier;
import lombok.AccessLevel;
import lombok.Setter;
import lombok.SneakyThrows;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import lombok.val;

import java.io.IOException;
import java.nio.file.*;
Expand Down Expand Up @@ -53,8 +50,8 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) th
new WatchEvent.Kind[]{ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY},
SensitivityWatchEventModifier.HIGH);
WATCH_KEY_MAP.put(watchKey, dir);
log.debug("Registering {} in watcher service. WATCH_KEY_MAP size: {}", dir,
WATCH_KEY_MAP.values().size());
log.debug("WATCH_KEY_MAP size: {}. Registering into watcher service. Path: {}",
WATCH_KEY_MAP.values().size(), dir);
return FileVisitResult.CONTINUE;
}
});
Expand All @@ -63,7 +60,11 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) th
}
};
@Setter(AccessLevel.NONE)
@Getter
private volatile boolean terminated = false;
@Setter(AccessLevel.NONE)
@Getter(AccessLevel.PACKAGE)
private final Object terminateLock = new Object();
private final Path monitoredPath;
private FileWatcherHandler fileWatcherHandler;

Expand Down Expand Up @@ -157,10 +158,9 @@ private void monitorRecursively() {
*
* @author Johnny Miller (锺俊), email: [email protected], date: 10/27/2020 10:01 AM
*/
@SneakyThrows
public void destroy() {
public void terminate() {
WatchServiceSingleton.close();
synchronized (this) {
synchronized (terminateLock) {
this.terminated = true;
}
this.shutdownAndAwaitTermination();
Expand All @@ -178,7 +178,7 @@ public void destroy() {
private void shutdownAndAwaitTermination() {
// Disable new tasks from being submitted
THREAD_POOL.shutdown();
log.debug("Shutdown THREAD_POOL for FileWatcher done.");
log.debug("Shutdown THREAD_POOL for FileWatcher done, disabled new tasks from being submitted.");
try {
// Wait a while for existing tasks to terminate
if (!THREAD_POOL.awaitTermination(INTERVAL, TimeUnit.SECONDS)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public static WatchService getInstance() {
}
}
}
log.debug("WatchService is not null, returned directly");
return singletonInstance;
}

Expand Down

0 comments on commit fe41844

Please sign in to comment.