只是在这里搞乱,使用循环缓冲区。这是一个明智的实现,还是有更快/更可靠的方法给这只猫剥皮?
class CircularBuffer[T](size: Int)(implicit mf: Manifest[T]) {
private val arr = new scala.collection.mutable.ArrayBuffer[T]()
private var cursor = 0
val monitor = new ReentrantReadWriteLock()
def push(value: T) {
monitor.writeLock().lock()
try {
arr(cursor) = value
cursor += 1
cursor %= size
} finally {
monitor.writeLock().unlock()
}
}
def getAll: Array[T] = {
monitor.readLock().lock()
try {
val copy = new Array[T](size)
arr.copyToArray(copy)
copy
} finally {
monitor.readLock().unlock()
}
}
}