Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

32
LINES

< > BotCompany Repo | #1019344 // jdk_watchService_start - starts background thread

JavaX fragment (include)

scope jdk_watchService_start.

static new Flag #started;
static new MultiMap<WatchKey, O> #listeners; // values: voidfunc(File)
static int #interval = 50; // larger interval helps show less duplicate events (https://stackoverflow.com/questions/16777869/java-7-watchservice-ignoring-multiple-occurrences-of-the-same-event/34818620#34818620)

static WatchService jdk_watchService_start() {
  final WatchService service = jdk_defaultWatchService();
  if (started.raise()) thread "Watch Service" {
    while licensed { 
      WatchKey key = service.poll(10, TimeUnit.SECONDS);
      if (key != null) {
        Path dir = cast key.watchable();
        sleep(interval);
        L<WatchEvent<?>> events = key.pollEvents();
        new LinkedHashSet<File> changedFiles;
        for (WatchEvent<?> event : reversedIterator(events)) pcall {
          final Path changed = (Path) event.context();
          File full = newFile(dir.toFile(), changed.toFile().getPath());
          changedFiles.add(full);
          //print("WatchService Changed: " + full + ", " + event.kind());
        }
        for (File f : changedFiles)
          pcallFAll(listeners.get(key), f);
        key.reset();
      }
    }
  }
  ret service;
}

end scope

Author comment

Began life as a copy of #1019342

download  show line numbers  debug dex  old transpilations   

Travelled to 12 computer(s): bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1019344
Snippet name: jdk_watchService_start - starts background thread
Eternal ID of this version: #1019344/13
Text MD5: 5de6931fa605fa97544942e7a352ca1c
Author: stefan
Category: javax / io
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-12-07 22:36:44
Source code size: 1254 bytes / 32 lines
Pitched / IR pitched: No / No
Views / Downloads: 266 / 329
Version history: 12 change(s)
Referenced in: [show references]