Last active
March 23, 2017 16:17
-
-
Save kevinherron/3b0b882b941c4eb37b123082b05a865e to your computer and use it in GitHub Desktop.
LongAdderCounter and LongAccumulatorCounter
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; | |
import java.util.concurrent.atomic.LongAccumulator; | |
public class LongAccumulatorCounter extends ServerCounter { | |
private final AtomicReference<LongAccumulator> accumulator = new AtomicReference<>( | |
new LongAccumulator( | |
(left, right) -> left + right, | |
0L | |
) | |
); | |
@Override | |
public void add(long increment) { | |
accumulator.get().accumulate(increment); | |
} | |
@Override | |
public long getAndReset() { | |
LongAccumulator prev = accumulator.getAndSet( | |
new LongAccumulator( | |
(left, right) -> left + right, | |
0L | |
) | |
); | |
return prev.get(); | |
} | |
} |
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; | |
import java.util.concurrent.atomic.LongAdder; | |
public class LongAdderCounter extends ServerCounter { | |
private final AtomicReference<LongAdder> adder = new AtomicReference<>(new LongAdder()); | |
@Override | |
public void add(long increment) { | |
adder.get().add(increment); | |
} | |
@Override | |
public long getAndReset() { | |
LongAdder prev = adder.getAndSet(new LongAdder()); | |
return prev.sum(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment