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

25
LINES

< > BotCompany Repo | #1013541 // concurrentlyIterateValues - synchronizes on map

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (2606L/16K).

// iterate safely (& quickly) in the face of concurrent modifications
static <A, B> IterableIterator<B> concurrentlyIterateValues(NavigableMap<A, B> map) {
  ret concurrentlyIterateValues(map, map);
}

static <A, B> IterableIterator<B> concurrentlyIterateValues(final NavigableMap<A, B> map, O mutex) {
  ret iteratorFromFunction(new F0<B>() {
    Iterator<A> it = keys(map).iterator();
    A key;
    
    B get() {
      synchronized(mutex) {
        try {
          if (!it.hasNext()) null;
          ret map.get(key = it.next());
        } catch (ConcurrentModificationException e) {
          print("Re-iterating");
          it = map.tailMap(key, false).keySet().iterator();
          if (!it.hasNext()) null;
          ret map.get(key = it.next()); // Can't throw another exception
        }
      }
    }
  });
}

Author comment

Began life as a copy of #1012173

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1013541
Snippet name: concurrentlyIterateValues - synchronizes on map
Eternal ID of this version: #1013541/3
Text MD5: b97a10eaf1b6fd4b692b82e508fa25be
Transpilation MD5: 5665a8f4bec4f7bea84f0ab971636b6a
Author: stefan
Category: javax / collections
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-06-13 12:13:39
Source code size: 844 bytes / 25 lines
Pitched / IR pitched: No / No
Views / Downloads: 476 / 551
Version history: 2 change(s)
Referenced in: [show references]