Last active
March 8, 2017 13:53
-
-
Save dpsoft/8453bf894e3c7f907b543ccda5a602a9 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.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