Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- import java.util.function.Function;
- public class SimpleNonBlockingLockList<T> {
- private final List<T> elements = new ArrayList<T>();
- private final List<T> addQueue = Collections.synchronizedList(new ArrayList<T>());
- private final List<T> removeQueue = Collections.synchronizedList(new ArrayList<T>());
- public void add(T t) {
- synchronized (addQueue) {
- addQueue.add(t);
- }
- }
- public void remove(T t) {
- synchronized (removeQueue) {
- removeQueue.add(t);
- }
- }
- public synchronized void updateWith(Function<T, Void> function) {
- elements.addAll(addQueue);
- elements.removeAll(removeQueue);
- for (T element : elements) {
- function.apply(element);
- }
- }
- }
Advertisement