Created
December 19, 2011 22:02
-
-
Save makotan/1499102 to your computer and use it in GitHub Desktop.
低頻度の更新と高頻度の読み込みがある場合・・・
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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