Skip to content

Instantly share code, notes, and snippets.

@xjia1
Last active December 10, 2015 20:48
Show Gist options
  • Save xjia1/4490196 to your computer and use it in GitHub Desktop.
Save xjia1/4490196 to your computer and use it in GitHub Desktop.
The cost of locks
import java.util.concurrent.atomic.AtomicLong;
public class CostOfLocksThreaded {
private static class Incr extends Thread {
private static long a;
public static void initialize() { a = 0; }
public static long get() { return a; }
private long incr() { return ++a; }
public void run() {
long beginTime = System.currentTimeMillis();
for (long i = 0; i < 500000000; ++i) incr();
duration = System.currentTimeMillis() - beginTime;
}
public long duration;
}
private static class IncrWithLock extends Thread {
private static Object lock = new Object();
private static long a;
public static void initialize() { a = 0; }
public static long get() { return a; }
private long incr() { synchronized(lock){ return ++a; } }
public void run() {
long beginTime = System.currentTimeMillis();
for (long i = 0; i < 500000000; ++i) incr();
duration = System.currentTimeMillis() - beginTime;
}
public long duration;
}
private static class IncrWithCAS extends Thread {
private static AtomicLong a;
public static void initialize() { a = new AtomicLong(); }
public static AtomicLong get() { return a; }
private long incr() { return a.incrementAndGet(); }
public void run() {
long beginTime = System.currentTimeMillis();
for (long i = 0; i < 500000000; ++i) incr();
duration = System.currentTimeMillis() - beginTime;
}
public long duration;
}
private static class IncrWithVolatileWrite extends Thread {
private static volatile long a;
public static void initialize() { a = 0; }
public static long get() { return a; }
private long incr() { return ++a; }
public void run() {
long beginTime = System.currentTimeMillis();
for (long i = 0; i < 500000000; ++i) incr();
duration = System.currentTimeMillis() - beginTime;
}
public long duration;
}
private void testIncr() throws Exception {
Incr.initialize();
System.out.print("incr:");
final Incr[] t = new Incr[n];
for (int i = 0; i < n; i++) t[i] = new Incr();
for (int i = 0; i < n; i++) t[i].start();
for (int i = 0; i < n; i++) t[i].join();
for (int i = 0; i < n; i++) System.out.print(" " + t[i].duration);
System.out.println();
System.out.println(Incr.get());
}
private void testIncrWithLock() throws Exception {
IncrWithLock.initialize();
System.out.print("incrWithLock:");
final IncrWithLock[] t = new IncrWithLock[n];
for (int i = 0; i < n; i++) t[i] = new IncrWithLock();
for (int i = 0; i < n; i++) t[i].start();
for (int i = 0; i < n; i++) t[i].join();
for (int i = 0; i < n; i++) System.out.print(" " + t[i].duration);
System.out.println();
System.out.println(IncrWithLock.get());
}
private void testIncrWithCAS() throws Exception {
IncrWithCAS.initialize();
System.out.print("incrWithCAS:");
final IncrWithCAS[] t = new IncrWithCAS[n];
for (int i = 0; i < n; i++) t[i] = new IncrWithCAS();
for (int i = 0; i < n; i++) t[i].start();
for (int i = 0; i < n; i++) t[i].join();
for (int i = 0; i < n; i++) System.out.print(" " + t[i].duration);
System.out.println();
System.out.println(IncrWithCAS.get());
}
private void testIncrWithVolatileWrite() throws Exception {
IncrWithVolatileWrite.initialize();
System.out.print("incrWithVolatileWrite:");
final IncrWithVolatileWrite[] t = new IncrWithVolatileWrite[n];
for (int i = 0; i < n; i++) t[i] = new IncrWithVolatileWrite();
for (int i = 0; i < n; i++) t[i].start();
for (int i = 0; i < n; i++) t[i].join();
for (int i = 0; i < n; i++) System.out.print(" " + t[i].duration);
System.out.println();
System.out.println(IncrWithVolatileWrite.get());
}
public void run() throws Exception {
testIncr();
testIncrWithLock();
testIncrWithCAS();
testIncrWithVolatileWrite();
}
private static int n;
public static void main(String[] args) throws Exception {
for (n = 1; n <= 4; n++) {
for (int i = 0; i < 3; i++) {
System.out.println("n: " + n);
final CostOfLocksThreaded test = new CostOfLocksThreaded();
test.run();
System.out.println();
}
}
}
}
Intel(R) Core(TM)2 Duo CPU E6550 @ 2.33GHz
Linux 3.5.0-21-generic
Ubuntu Server 12.10 64-bit
javac 1.7.0_09
java version "1.7.0_09"
OpenJDK Runtime Environment (IcedTea7 2.3.3) (7u9-2.3.3-0ubuntu1~12.10.1)
OpenJDK 64-Bit Server VM (build 23.2-b09, mixed mode)
n: 1
incr: 437
500000000
incrWithLock: 19098
500000000
incrWithCAS: 8750
500000000
incrWithVolatileWrite: 7374
500000000
n: 1
incr: 430
500000000
incrWithLock: 19094
500000000
incrWithCAS: 8739
500000000
incrWithVolatileWrite: 7371
500000000
n: 1
incr: 430
500000000
incrWithLock: 19105
500000000
incrWithCAS: 8738
500000000
incrWithVolatileWrite: 7367
500000000
n: 2
incr: 430 432
500042175
incrWithLock: 36814 35531
1000000000
incrWithCAS: 45497 45721
1000000000
incrWithVolatileWrite: 20940 20973
503256668
n: 2
incr: 432 430
500000000
incrWithLock: 35341 36102
1000000000
incrWithCAS: 45221 45070
1000000000
incrWithVolatileWrite: 20987 20972
502487427
n: 2
incr: 434 430
500000000
incrWithLock: 42071 41633
1000000000
incrWithCAS: 46039 45988
1000000000
incrWithVolatileWrite: 20950 20978
502986510
n: 3
incr: 431 647 640
511299560
incrWithLock: 62402 61490 62249
1500000000
incrWithCAS: 68982 68867 67986
1500000000
incrWithVolatileWrite: 31381 31152 31040
631167962
n: 3
incr: 622 509 670
510620080
incrWithLock: 62156 62365 62509
1500000000
incrWithCAS: 69075 68833 68793
1500000000
incrWithVolatileWrite: 30962 30436 31324
632283345
n: 3
incr: 602 695 515
500043976
incrWithLock: 61848 61837 61830
1500000000
incrWithCAS: 68840 69071 64879
1500000000
incrWithVolatileWrite: 30107 31201 30546
654855475
n: 4
incr: 857 851 860 850
509038095
incrWithLock: 83018 82921 82068 82147
2000000000
incrWithCAS: 91314 92061 91245 92037
2000000000
incrWithVolatileWrite: 41745 41977 41675 41953
689449918
n: 4
incr: 851 847 850 843
517185228
incrWithLock: 88837 89826 89666 88178
2000000000
incrWithCAS: 92163 92191 92037 92134
2000000000
incrWithVolatileWrite: 41879 41954 41934 41949
689283447
n: 4
incr: 683 860 858 791
507567639
incrWithLock: 87246 84316 76373 86845
2000000000
incrWithCAS: 91027 90976 91024 91017
2000000000
incrWithVolatileWrite: 41958 41965 41965 41933
680400753
Intel(R) Xeon(R) CPU E5620 @ 2.40GHz
Linux 2.6.18-308.20.1.el5
javac 1.6.0_23
java version "1.6.0_23"
Java(TM) SE Runtime Environment (build 1.6.0_23-b05)
Java HotSpot(TM) 64-Bit Server VM (build 19.0-b09, mixed mode)
n: 1
incr: 774
500000000
incrWithLock: 13317
500000000
incrWithCAS: 5807
500000000
incrWithVolatileWrite: 4753
500000000
n: 1
incr: 897
500000000
incrWithLock: 13679
500000000
incrWithCAS: 5704
500000000
incrWithVolatileWrite: 4970
500000000
n: 1
incr: 607
500000000
incrWithLock: 13667
500000000
incrWithCAS: 6222
500000000
incrWithVolatileWrite: 4797
500000000
n: 2
incr: 386 632
500045931
incrWithLock: 106770 97790
1000000000
incrWithCAS: 34628 31553
1000000000
incrWithVolatileWrite: 40168 40949
572816980
n: 2
incr: 630 435
500000000
incrWithLock: 116501 123637
1000000000
incrWithCAS: 32300 34679
1000000000
incrWithVolatileWrite: 40534 41313
572242505
n: 2
incr: 594 381
500025557
incrWithLock: 152081 155733
1000000000
incrWithCAS: 33400 35716
1000000000
incrWithVolatileWrite: 40137 40935
571863666
n: 3
incr: 602 389 388
500021643
incrWithLock: 226971 210327 227564
1500000000
incrWithCAS: 52073 64647 63672
1500000000
incrWithVolatileWrite: 40410 54594 54866
791478981
n: 3
incr: 569 382 629
500705721
incrWithLock: 227962 261049 261948
1500000000
incrWithCAS: 51985 63606 64565
1500000000
incrWithVolatileWrite: 41378 54958 55210
790757756
n: 3
incr: 579 386 385
500000000
incrWithLock: 222471 222699 130304
1500000000
incrWithCAS: 66982 67095 66929
1500000000
incrWithVolatileWrite: 40146 54899 54623
791891615
n: 4
incr: 522 387 608 385
500351449
incrWithLock: 539678 500353 540522 492997
2000000000
incrWithCAS: 89135 91765 91988 89230
2000000000
incrWithVolatileWrite: 59114 64176 64097 59110
1413805442
n: 4
incr: 520 542 390 388
500011501
incrWithLock: 533722 533194 525230 493378
2000000000
incrWithCAS: 88987 90266 90379 89063
2000000000
incrWithVolatileWrite: 59177 64282 59178 64200
1416414786
n: 4
incr: 415 419 387 384
500000000
incrWithLock: 362494 322530 403080 400066
2000000000
incrWithCAS: 90332 85159 85132 89878
2000000000
incrWithVolatileWrite: 59522 64386 64481 59550
1421194724
1.6GHz Intel Core i5
Darwin Kernel Version 11.4.2
Mac OS X 10.7.5
javac 1.6.0_37
java version "1.6.0_37"
Java(TM) SE Runtime Environment (build 1.6.0_37-b06-434-11M3909)
Java HotSpot(TM) 64-Bit Server VM (build 20.12-b01-434, mixed mode)
n: 1
incr: 497
500000000
incrWithLock: 19221
500000000
incrWithCAS: 8511
500000000
incrWithVolatileWrite: 5332
500000000
n: 1
incr: 503
500000000
incrWithLock: 19190
500000000
incrWithCAS: 8466
500000000
incrWithVolatileWrite: 5325
500000000
n: 1
incr: 452
500000000
incrWithLock: 19188
500000000
incrWithCAS: 8465
500000000
incrWithVolatileWrite: 5207
500000000
n: 2
incr: 669 670
500093852
incrWithLock: 66215 66463
1000000000
incrWithCAS: 53002 52957
1000000000
incrWithVolatileWrite: 20642 20644
662847439
n: 2
incr: 622 622
500000000
incrWithLock: 68405 68185
1000000000
incrWithCAS: 50946 50975
1000000000
incrWithVolatileWrite: 20826 20816
663211959
n: 2
incr: 636 626
500000000
incrWithLock: 67734 67920
1000000000
incrWithCAS: 55497 55500
1000000000
incrWithVolatileWrite: 19779 19786
664072722
n: 3
incr: 774 766 783
500539475
incrWithLock: 92732 92465 90841
1500000000
incrWithCAS: 163984 164042 163313
1500000000
incrWithVolatileWrite: 48431 48542 48510
688495986
n: 3
incr: 824 747 723
500000000
incrWithLock: 92690 91189 91657
1500000000
incrWithCAS: 158182 158854 158695
1500000000
incrWithVolatileWrite: 49321 49310 49315
701201906
n: 3
incr: 774 784 777
500763537
incrWithLock: 93346 93238 92799
1500000000
incrWithCAS: 153176 152938 153330
1500000000
incrWithVolatileWrite: 48005 48227 48165
704783180
n: 4
incr: 1036 1078 967 1116
503323507
incrWithLock: 123602 122931 123006 123565
2000000000
incrWithCAS: 213849 214027 214447 214455
2000000000
incrWithVolatileWrite: 52711 52722 52462 52692
741510433
n: 4
incr: 1180 1107 1006 1090
500000000
incrWithLock: 121187 120843 121045 120285
2000000000
incrWithCAS: 224795 223905 224843 224775
2000000000
incrWithVolatileWrite: 52954 53034 52977 53025
743273034
n: 4
incr: 940 985 1073 1033
500198699
incrWithLock: 122935 121136 119888 122726
2000000000
incrWithCAS: 231371 230813 231358 231316
2000000000
incrWithVolatileWrite: 53453 52944 53306 53438
767448286
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment