Skip to content

Instantly share code, notes, and snippets.

@lagenorhynque
Created December 5, 2017 15:02
Show Gist options
  • Select an option

  • Save lagenorhynque/848f2b8ef6d2b67f3d669a8329cd685c to your computer and use it in GitHub Desktop.

Select an option

Save lagenorhynque/848f2b8ef6d2b67f3d669a8329cd685c to your computer and use it in GitHub Desktop.
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();
}
}
//--------------------------------------------------------------------------------
}
@lagenorhynque

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment