I am trying to create a small test (in Groovy) that shows a high thread conflict for a couple of synchronized methods. A high level of competition should be shown when monitoring voluntary context switches, and on Linux this can be achieved thanks to "pidstat".
The program is as follows:
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"
}
}).start()
}
while (res.getN() < N) {
}
println "========================="
}
but the team
pidstat -w -I -p 26848 5
prints 0 on an arbitrary context radio button column. The program creates a thread 100000, which simultaneously refers to the synchronized method. I cannot believe that under such a load context switching does not occur.
What happened to my benchmark?
agori source
share