Skip to content

Instantly share code, notes, and snippets.

@makotan
Created December 19, 2011 22:02
Show Gist options
  • Save makotan/1499102 to your computer and use it in GitHub Desktop.
Save makotan/1499102 to your computer and use it in GitHub Desktop.
低頻度の更新と高頻度の読み込みがある場合・・・
import java.util.concurrent.atomic.AtomicReference
class LowUpdateHighReadList[T] {
val aList = new AtomicReference[List[T]]
aList.set(List())
def list() : List[T] = aList.get()
def add(t : T) : List[T] = {
listUpdate(t::_)
}
def remove(t : T) : List[T] = {
listUpdate(_.filter(_ != t))
}
protected def listUpdate(f : (List[T]) => List[T]) : List[T] = {
val list = aList.get()
val newList = f(list)
if( aList.compareAndSet(list,newList) ) {
newList
} else {
listUpdate(f)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment