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).

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

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: 477 / 552
Version history: 2 change(s)
Referenced in: [show references]