Skip to content

Instantly share code, notes, and snippets.

@prydin
Last active December 19, 2019 21:03
Embed
What would you like to do?
A simple benchmark of synchronization
public class Test {
public static void main(String[] args) {
int value = 0;
long now = System.currentTimeMillis();
for(int i = 0; i < 1e8; i++) {
value++;
}
System.out.println("Unsynched version took " + (System.currentTimeMillis() - now) + "ms");
Object syncher = new Object();
now = System.currentTimeMillis();
for(int i = 0; i < 1e8; i++) {
synchronized(syncher) {
value++;
}
}
System.out.println("Synched version took " + (System.currentTimeMillis() - now) + "ms");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment