Skip to content

Instantly share code, notes, and snippets.

@dpsoft
Last active March 8, 2017 13:53
Show Gist options
  • Save dpsoft/8453bf894e3c7f907b543ccda5a602a9 to your computer and use it in GitHub Desktop.
Save dpsoft/8453bf894e3c7f907b543ccda5a602a9 to your computer and use it in GitHub Desktop.
import java.util.concurrent.atomic.AtomicLong
import scala.annotation.tailrec
class LongMaxUpdater(value:AtomicLong) {
def update(newMax:Long):Long = {
@tailrec def compare():Long = {
val currentMax = value.get()
if(newMax > currentMax) {
if (!value.compareAndSet(currentMax, newMax)) compare()
else newMax
} else currentMax
}
compare()
}
def get():Long = value.get()
def set(newValue:Long):Long = value.getAndSet(newValue)
}
object LongMaxUpdater {
def apply(): LongMaxUpdater = new LongMaxUpdater(new AtomicLong(0L))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment