Last active
August 29, 2015 14:12
-
-
Save arturmkrtchyan/2fa5e8089a5ba60d1cf7 to your computer and use it in GitHub Desktop.
Uncontended Biased Locking
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 BiasedLocking { | |
private static final int LOOP_COUNT = 10000000; // 10 million | |
public static void main(final String[] args) { | |
incrementCounter(); | |
} | |
public static void incrementCounter() { | |
final long startTime = System.currentTimeMillis(); | |
final Counter counter = new Counter(); | |
for (int i = 0; i < LOOP_COUNT; i++) { | |
counter.increment(); | |
} | |
final long endTime = System.currentTimeMillis(); | |
System.out.printf("Counter: %,d - Elapsed time: %d ms\n", | |
counter.getCount(), (endTime - startTime)); | |
} | |
private static class Counter { | |
private long count; | |
public synchronized void increment() { | |
count += 1; | |
} | |
public long getCount() { | |
return count; | |
} | |
} | |
} |
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
javac BiasedLocking.java && java -XX:BiasedLockingStartupDelay=0 BiasedLocking | |
Counter: 10,000,000 - Elapsed time: 24 ms |
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
javac BiasedLocking.java && java -XX:-UseBiasedLocking BiasedLocking | |
Counter: 10,000,000 - Elapsed time: 209 ms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment