Created
June 17, 2017 18:43
-
-
Save yonatang/774ae1a52c0fa62d4f2f51c7bea81511 to your computer and use it in GitHub Desktop.
Static counter in Java
This file contains 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.ConcurrentHashMap; | |
import java.util.concurrent.atomic.AtomicInteger; | |
public class Counter { | |
private final static ConcurrentHashMap<String, AtomicInteger> map=new ConcurrentHashMap<>(); | |
public static int inc(String name){ | |
return map.computeIfAbsent(name, x -> new AtomicInteger()).getAndIncrement(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment