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

21
LINES

< > BotCompany Repo | #1012173 // concurrentlyIterateKeys - synchronizes on map

JavaX fragment (include)

// iterate safely (& quickly) in the face of concurrent modifications
static <A, B> IterableIterator<A> concurrentlyIterateKeys(final NavigableMap<A, B> map) {
  ret iteratorFromFunction(new F0<A>() {
    Iterator<A> it = keys(map).iterator();
    A key;
    
    A get() {
      synchronized(map) {
        try {
          if (!it.hasNext()) null;
          ret key = it.next();
        } catch (ConcurrentModificationException e) {
          print("Re-iterating");
          it = map.tailMap(key, false).keySet().iterator();
          if (!it.hasNext()) null;
          ret key = it.next(); // Can't throw another exception
        }
      }
    }
  });
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1012173
Snippet name: concurrentlyIterateKeys - synchronizes on map
Eternal ID of this version: #1012173/2
Text MD5: 970ac3137b5a8a1ae2ec478730a8aa00
Author: stefan
Category: javax / collections
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-01-08 08:49:19
Source code size: 677 bytes / 21 lines
Pitched / IR pitched: No / No
Views / Downloads: 397 / 418
Version history: 1 change(s)
Referenced in: [show references]