Skip to content

Instantly share code, notes, and snippets.

@prydin
Last active December 19, 2019 21:03
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save prydin/997d27f2413bbe2e8085b3a479b59d13 to your computer and use it in GitHub Desktop.
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