Skip to content

Instantly share code, notes, and snippets.

@adamv
Created August 10, 2011 18:40
Show Gist options
  • Save adamv/1137745 to your computer and use it in GitHub Desktop.
Save adamv/1137745 to your computer and use it in GitHub Desktop.
Is this dumb?
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicReference;
public class PairedList<T> {
private ConcurrentLinkedQueue<T> list1;
private ConcurrentLinkedQueue<T> list2;
private AtomicReference<ConcurrentLinkedQueue<T>> activeList;
public PairedList() {
this.list1 = new ConcurrentLinkedQueue<T>();
this.list2 = new ConcurrentLinkedQueue<T>();
this.activeList = new AtomicReference<ConcurrentLinkedQueue<T>>(list1);
}
public ConcurrentLinkedQueue<T> getActiveList() {
return activeList.get();
}
public boolean appendToActiveList(T value) {
return getActiveList().add(value);
}
public ConcurrentLinkedQueue<T> getAndSwapLists() {
if (activeList.compareAndSet(list1, list2)) {
return list1;
} else {
activeList.set(list1);
return list2;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment