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

23
LINES

< > BotCompany Repo | #1013140 // concurrentlyIterateSet

JavaX fragment (include)

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

Author comment

Began life as a copy of #1012173

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: #1013140
Snippet name: concurrentlyIterateSet
Eternal ID of this version: #1013140/4
Text MD5: 2ccd90b7cb6a7de305ce52f0072ab235
Author: stefan
Category: javax / collections
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-01-12 11:18:33
Source code size: 754 bytes / 23 lines
Pitched / IR pitched: No / No
Views / Downloads: 324 / 374
Version history: 3 change(s)
Referenced in: [show references]