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)

1  
// iterate safely (& quickly) in the face of concurrent modifications
2  
static <A, B> IterableIterator<A> concurrentlyIterateKeys(final NavigableMap<A, B> map) {
3  
  ret iteratorFromFunction(new F0<A>() {
4  
    Iterator<A> it = keys(map).iterator();
5  
    A key;
6  
    
7  
    A get() {
8  
      synchronized(map) {
9  
        try {
10  
          if (!it.hasNext()) null;
11  
          ret key = it.next();
12  
        } catch (ConcurrentModificationException e) {
13  
          print("Re-iterating");
14  
          it = map.tailMap(key, false).keySet().iterator();
15  
          if (!it.hasNext()) null;
16  
          ret key = it.next(); // Can't throw another exception
17  
        }
18  
      }
19  
    }
20  
  });
21  
}

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: 403 / 425
Version history: 1 change(s)
Referenced in: [show references]