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

23
LINES

< > TinyBrain | #1013140 - concurrentlyIterateSet

JavaX fragment (include)

// iterate safely (& quickly) in the face of concurrent modifications
static <A> IterableIterator<A> concurrentlyIterateSet(final NavigableSet<A> set) {
  fO mutex = collectionMutex(set);
  class ConcurrentlyIterateSet extends F0<A> {
    Iterator<A> it = set.iterator();
    A key;
    
    A get() {
      synchronized(mutex) {
        try {
          if (!it.hasNext()) null;
          ret key = it.next();
        } catch (ConcurrentModificationException e) {
          print("Re-iterating");
          it = set.tailSet(key, false).iterator();
          if (!it.hasNext()) null;
          ret key = it.next(); // Can't throw another exception
        }
      }
    }
  }
  ret iteratorFromFunction(new ConcurrentlyIterateSet);
}

Author comment

Began life as a copy of #1012173

download  show line numbers  debug dex   

Travelled to 6 computer(s): cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, mqqgnosmbjvj, tslmcundralx, tvejysmllsmz

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: 42 / 50
Version history: 3 change(s)
Referenced in: [show]