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

40
LINES

< > BotCompany Repo | #1034557 // MoreEfficientCopyOnWriteList - a better CopyOnWriteArrayList that only makes a copy when there is no iterator

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (5279L/29K).

// synchronized
sclass MoreEfficientCopyOnWriteList<A> extends NotifyingList<A> {
  bool hasIterator;
  
  *() { this(new L); }
  
  // Note: uses list as the inner list!
  *(L<A> list) { super(list); }
  
  public synchronized Iterator<A> iterator() {
    set hasIterator;
    ret super.iterator();
  }
  
  public synchronized Spliterator<A> spliterator() {
    set hasIterator;
    ret super.spliterator();
  }
  
  public synchronized ListIterator<A> listIterator() {
    set hasIterator;
    ret super.listIterator();
  }

  public synchronized ListIterator<A> listIterator(int index) {
    set hasIterator;
    ret super.listIterator(index);
  }
  
  @Override
  // This is called in synchronized block
  void beforeChange {
    if (hasIterator) {
      setInnerList(cloneList(list));
      hasIterator = false;
    }
  }
  
  void change {}
}

Author comment

Began life as a copy of #1033756

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034557
Snippet name: MoreEfficientCopyOnWriteList - a better CopyOnWriteArrayList that only makes a copy when there is no iterator
Eternal ID of this version: #1034557/11
Text MD5: 05fa41388e2bc75b3ee99446c138a4ac
Transpilation MD5: 1b0b301c141a41022c8c1cc3f0f3298c
Author: stefan
Category: javax / collections
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-02-13 18:40:34
Source code size: 888 bytes / 40 lines
Pitched / IR pitched: No / No
Views / Downloads: 142 / 250
Version history: 10 change(s)
Referenced in: [show references]