Monitoring lock contention on Java applications
I am trying to create a little benchmark (in Groovy) that shows high thread contention on a couple of synchronized methods. High contention should show up when monitoring voluntary context switches, and in Linux this can be achieved thanks to "pidstat". The program is the following: class Res { private int n; synchronized public void inc() { n++; def foo = [] for (int i = 0; i < 1000; ++i) foo << "hello" } synchronized public int getN() { return n; } } while (true) { Res res = new Res() int N = 100000 for (int i = 0; i < N; ++i) { new Thread({ res.inc() if (res.getN() == N) { println "ok" } })