Created
December 5, 2017 15:02
-
-
Save lagenorhynque/848f2b8ef6d2b67f3d669a8329cd685c 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.*; | |
| public class Bench { | |
| public static final int _keyRange = 100; | |
| public static final int _threadCount = 300; | |
| public static final int _repeat = 100000; | |
| //-------------------------------------------------------------------------------- | |
| public static void main(String[] args) throws Exception { | |
| final long start = System.currentTimeMillis(); | |
| final Map map = new HashMap(); | |
| Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { | |
| public void run() { | |
| System.out.println(System.currentTimeMillis() - start); | |
| System.out.println(map); | |
| } | |
| })); | |
| for (int i = 0; i < _threadCount; ++i) { | |
| new Thread(new Runnable() { | |
| public void run() { | |
| for (int k = 0; k < _repeat; ++k) { | |
| Random random = new Random(); | |
| String key = "key" + random.nextInt(_keyRange); | |
| synchronized(map) { | |
| Integer value = (Integer) map.get(key); | |
| if (value != null) { | |
| int oldValue = value.intValue(); | |
| map.put(key, new Integer(oldValue + 1)); | |
| } else { | |
| map.put(key, new Integer(1)); | |
| } | |
| } | |
| } | |
| } | |
| }).start(); | |
| } | |
| } | |
| //-------------------------------------------------------------------------------- | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cf. https://github.com/lagenorhynque/bench