Last active
December 19, 2019 21:03
-
-
Save prydin/997d27f2413bbe2e8085b3a479b59d13 to your computer and use it in GitHub Desktop.
A simple benchmark of synchronization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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