Created
November 27, 2018 03:54
-
-
Save piyo7/ef7e4c4ef59d2f96373edb043ede3e19 to your computer and use it in GitHub Desktop.
スレッドセーフな連想カウンタをリセットしたい人生だった ref: https://qiita.com/piyo7/items/81d1d395733b3cea072c
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
| val counter = TrieMap[Key, LongAdder]() |
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
| counter.getOrElseUpdate(key, new LongAdder()).increment() |
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
| for (key <- counter.keys) { | |
| val sum = counter.remove(key).sum() | |
| logger.debug(s"$key: $sum") | |
| } |
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
| for { | |
| key <- counter.keys | |
| value <- counter.get(key) | |
| } { | |
| val sum = value.sumThenReset() | |
| logger.debug(s"$key: $sum") | |
| } |
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
| val counter = TrieMap[Key, AtomicLong]() |
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
| for { | |
| key <- counter.keys | |
| value <- counter.get(key) | |
| } { | |
| val v = value.getAndSet(0) | |
| logger.debug(s"$key: $v") | |
| } |
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
| for { | |
| key <- counter.keys | |
| value <- counter.get(key) | |
| } { | |
| val v = value.get() | |
| if (v == 0) counter.remove(key) | |
| } |
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
| scala> new AtomicLong(0) == new AtomicLong(0) | |
| res0: Boolean = false |
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
| public final class AtomicLongMap<K> implements Serializable { | |
| private final ConcurrentHashMap<K, Long> map; |
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
| public final class AtomicLongMap<K> { | |
| private final ConcurrentHashMap<K, AtomicLong> map; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment