-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(Concurrency): improve Java object lock for FileWatcher::terminate()
improve Java object lock for FileWatcher::terminate() BREAKING CHANGE: improve Java object lock for FileWatcher::terminate()
- Loading branch information
1 parent
f1b9720
commit fe41844
Showing
3 changed files
with
14 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.*; | ||
|
@@ -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; | ||
} | ||
}); | ||
|
@@ -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; | ||
|
||
|
@@ -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(); | ||
|
@@ -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)) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters