synchronized 与 Lock 性能比较

JDK1.8

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
D:\soft\Java\jdk1.8.0_172\bin\java.exe "-javaagent:D:\soft\JetBrains\IntelliJ IDEA 2018.1.6\lib\idea_rt.jar=57167:D:\soft\JetBrains\IntelliJ IDEA 2018.1.6\bin" -Dfile.encoding=UTF-8 -classpath "D:\soft\Java\jdk1.8.0_172\jre\lib\charsets.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\deploy.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\ext\access-bridge-64.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\ext\cldrdata.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\ext\dnsns.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\ext\jaccess.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\ext\jfxrt.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\ext\localedata.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\ext\nashorn.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\ext\sunec.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\ext\sunjce_provider.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\ext\sunmscapi.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\ext\sunpkcs11.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\ext\zipfs.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\javaws.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\jce.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\jfr.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\jfxswt.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\jsse.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\management-agent.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\plugin.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\resources.jar;D:\soft\Java\jdk1.8.0_172\jre\lib\rt.jar;D:\myworkspace\03mygithub\MyThread\bin;D:\soft\JetBrains\IntelliJ IDEA 2018.1.6\lib\junit-4.12.jar;D:\soft\JetBrains\IntelliJ IDEA 2018.1.6\lib\hamcrest-core-1.3.jar" com.zc.concurrent.issue.ReentrantLockVsSynchronizedTest
-------------------------------------
// 线程数1,每个线程循环 100000 次
3789172(elapse), testLongAdder: result=100000, threadCount=1, loopCount=100000
6642633(elapse), testReentrantLockUnfair: result=100000, threadCount=1, loopCount=100000
6841152(elapse), testReentrantLockFair: result=100000, threadCount=1, loopCount=100000
8382098(elapse), testSynchronized: result=100000, threadCount=1, loopCount=100000
44980066(elapse), testAtomicInteger: result=100000, threadCount=1, loopCount=100000
-------------------------------------
// 线程数2,每个线程循环 100000 次
2647611(elapse), testLongAdder: result=300000, threadCount=2, loopCount=100000
3772457(elapse), testAtomicInteger: result=300000, threadCount=2, loopCount=100000
5013131(elapse), testSynchronized: result=300000, threadCount=2, loopCount=100000
5831547(elapse), testReentrantLockFair: result=300000, threadCount=2, loopCount=100000
9820412(elapse), testReentrantLockUnfair: result=300000, threadCount=2, loopCount=100000
-------------------------------------
// 线程数4,每个线程循环 100000 次
2838213(elapse), testSynchronized: result=700000, threadCount=4, loopCount=100000
3695923(elapse), testAtomicInteger: result=700000, threadCount=4, loopCount=100000
12967987(elapse), testLongAdder: result=700000, threadCount=4, loopCount=100000
13843291(elapse), testReentrantLockUnfair: result=700000, threadCount=4, loopCount=100000
1132170308(elapse), testReentrantLockFair: result=700000, threadCount=4, loopCount=100000
-------------------------------------
// 线程数6,每个线程循环 100000 次
2340008(elapse), testLongAdder: result=1300000, threadCount=6, loopCount=100000
5598133(elapse), testSynchronized: result=1300000, threadCount=6, loopCount=100000
11595358(elapse), testAtomicInteger: result=1300000, threadCount=6, loopCount=100000
17305800(elapse), testReentrantLockUnfair: result=1300000, threadCount=6, loopCount=100000
1706668497(elapse), testReentrantLockFair: result=1300000, threadCount=6, loopCount=100000
-------------------------------------
// 线程数8,每个线程循环 100000 次
2604212(elapse), testLongAdder: result=2100000, threadCount=8, loopCount=100000
7267222(elapse), testSynchronized: result=2100000, threadCount=8, loopCount=100000
15585101(elapse), testAtomicInteger: result=2100000, threadCount=8, loopCount=100000
20813467(elapse), testReentrantLockUnfair: result=2100000, threadCount=8, loopCount=100000
2392860159(elapse), testReentrantLockFair: result=2100000, threadCount=8, loopCount=100000
-------------------------------------
// 线程数10,每个线程循环 100000 次
6970763(elapse), testLongAdder: result=3100000, threadCount=10, loopCount=100000
9930961(elapse), testSynchronized: result=3100000, threadCount=10, loopCount=100000
20397367(elapse), testAtomicInteger: result=3100000, threadCount=10, loopCount=100000
25850936(elapse), testReentrantLockUnfair: result=3100000, threadCount=10, loopCount=100000
2794116491(elapse), testReentrantLockFair: result=3100000, threadCount=10, loopCount=100000
-------------------------------------
// 线程数50,每个线程循环 100000 次
14844099(elapse), testLongAdder: result=8100000, threadCount=50, loopCount=100000
47918859(elapse), testSynchronized: result=8100000, threadCount=50, loopCount=100000
98661562(elapse), testAtomicInteger: result=8100000, threadCount=50, loopCount=100000
126939598(elapse), testReentrantLockUnfair: result=8100000, threadCount=50, loopCount=100000
14410074177(elapse), testReentrantLockFair: result=8100000, threadCount=50, loopCount=100000
-------------------------------------
// 线程数100,每个线程循环 100000 次
29294384(elapse), testLongAdder: result=18100000, threadCount=100, loopCount=100000
90143110(elapse), testSynchronized: result=18100000, threadCount=100, loopCount=100000
193961488(elapse), testAtomicInteger: result=18100000, threadCount=100, loopCount=100000
244101889(elapse), testReentrantLockUnfair: result=18100000, threadCount=100, loopCount=100000
31166365222(elapse), testReentrantLockFair: result=18100000, threadCount=100, loopCount=100000
-------------------------------------
// 线程数200,每个线程循环 100000 次
69702637(elapse), testLongAdder: result=38100000, threadCount=200, loopCount=100000
170973104(elapse), testSynchronized: result=38100000, threadCount=200, loopCount=100000
341206964(elapse), testAtomicInteger: result=38100000, threadCount=200, loopCount=100000
489087584(elapse), testReentrantLockUnfair: result=38100000, threadCount=200, loopCount=100000
63709112896(elapse), testReentrantLockFair: result=38100000, threadCount=200, loopCount=100000
-------------------------------------
// 线程数500,每个线程循环 100000 次
133355267(elapse), testLongAdder: result=88100000, threadCount=500, loopCount=100000
465237384(elapse), testSynchronized: result=88100000, threadCount=500, loopCount=100000
982221507(elapse), testAtomicInteger: result=88100000, threadCount=500, loopCount=100000
1246923387(elapse), testReentrantLockUnfair: result=88100000, threadCount=500, loopCount=100000
163173197284(elapse), testReentrantLockFair: result=88100000, threadCount=500, loopCount=100000
-------------------------------------
-------------------------------------
// 线程数500,每个线程循环 10000 次
46909254(elapse), testSynchronized: result=93100000, threadCount=500, loopCount=10000
53434593(elapse), testLongAdder: result=93100000, threadCount=500, loopCount=10000
76151149(elapse), testAtomicInteger: result=93100000, threadCount=500, loopCount=10000
115951527(elapse), testReentrantLockUnfair: result=93100000, threadCount=500, loopCount=10000
16067416290(elapse), testReentrantLockFair: result=93100000, threadCount=500, loopCount=10000
-------------------------------------
// 线程数500,每个线程循环 1000 次
21785245(elapse), testLongAdder: result=93600000, threadCount=500, loopCount=1000
22926219(elapse), testSynchronized: result=93600000, threadCount=500, loopCount=1000
23726455(elapse), testAtomicInteger: result=93600000, threadCount=500, loopCount=1000
23893305(elapse), testReentrantLockUnfair: result=93600000, threadCount=500, loopCount=1000
1583798403(elapse), testReentrantLockFair: result=93600000, threadCount=500, loopCount=1000
-------------------------------------
// 线程数500,每个线程循环 100 次
21886704(elapse), testSynchronized: result=93650000, threadCount=500, loopCount=100
22070561(elapse), testReentrantLockUnfair: result=93650000, threadCount=500, loopCount=100
22847925(elapse), testLongAdder: result=93650000, threadCount=500, loopCount=100
24225539(elapse), testAtomicInteger: result=93650000, threadCount=500, loopCount=100
133848487(elapse), testReentrantLockFair: result=93650000, threadCount=500, loopCount=100
-------------------------------------
// 线程数500,每个线程循环 10 次
20742504(elapse), testSynchronized: result=93655000, threadCount=500, loopCount=10
21348326(elapse), testLongAdder: result=93655000, threadCount=500, loopCount=10
21286160(elapse), testReentrantLockUnfair: result=93655000, threadCount=500, loopCount=10
21943591(elapse), testAtomicInteger: result=93655000, threadCount=500, loopCount=10
22087863(elapse), testReentrantLockFair: result=93655000, threadCount=500, loopCount=10
-------------------------------------
// 线程数500,每个线程循环 1 次
20936918(elapse), testReentrantLockUnfair: result=93655500, threadCount=500, loopCount=1
21259769(elapse), testAtomicInteger: result=93655500, threadCount=500, loopCount=1
21328093(elapse), testLongAdder: result=93655500, threadCount=500, loopCount=1
22007810(elapse), testSynchronized: result=93655500, threadCount=500, loopCount=1
23318273(elapse), testReentrantLockFair: result=93655500, threadCount=500, loopCount=1
-------------------------------------
// 线程数1000,每个线程循环 1 次
53460398(elapse), testLongAdder: result=1500, threadCount=1000, loopCount=1
58974079(elapse), testAtomicInteger: result=1500, threadCount=1000, loopCount=1
82545706(elapse), testSynchronized: result=1500, threadCount=1000, loopCount=1
130621152(elapse), testReentrantLockFair: result=1500, threadCount=1000, loopCount=1
200410000(elapse), testReentrantLockUnfair: result=1500, threadCount=1000, loopCount=1
-------------------------------------
// 线程数1500,每个线程循环 1 次
84837038(elapse), testSynchronized: result=3000, threadCount=1500, loopCount=1
104347079(elapse), testAtomicInteger: result=3000, threadCount=1500, loopCount=1
109697722(elapse), testReentrantLockUnfair: result=3000, threadCount=1500, loopCount=1
138776874(elapse), testReentrantLockFair: result=3000, threadCount=1500, loopCount=1
212777150(elapse), testLongAdder: result=3000, threadCount=1500, loopCount=1
-------------------------------------

Process finished with exit code 0

从上面的测试数据能看出来,在JDK1.8下synchronized 比 ReentrantLock 性能要好很多,从上面数据来看正常能快个3~5倍这样.

ReentrantLock 公平模式性能很差,并发竞争大的情况下,从数据看 正常比 非公平模式能慢两个数量级.

AtomicInteger 和 LongAdder 在这种场景下性能还是比 ReentrantLock 要好一些的.

JDK1.7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
D:\soft\Java\JDK1.7\bin\java.exe "-javaagent:D:\soft\JetBrains\IntelliJ IDEA 2018.1.6\lib\idea_rt.jar=60620:D:\soft\JetBrains\IntelliJ IDEA 2018.1.6\bin" -Dfile.encoding=UTF-8 -classpath D:\soft\Java\JDK1.7\jre\lib\charsets.jar;D:\soft\Java\JDK1.7\jre\lib\deploy.jar;D:\soft\Java\JDK1.7\jre\lib\ext\access-bridge-64.jar;D:\soft\Java\JDK1.7\jre\lib\ext\dnsns.jar;D:\soft\Java\JDK1.7\jre\lib\ext\jaccess.jar;D:\soft\Java\JDK1.7\jre\lib\ext\localedata.jar;D:\soft\Java\JDK1.7\jre\lib\ext\sunec.jar;D:\soft\Java\JDK1.7\jre\lib\ext\sunjce_provider.jar;D:\soft\Java\JDK1.7\jre\lib\ext\sunmscapi.jar;D:\soft\Java\JDK1.7\jre\lib\ext\zipfs.jar;D:\soft\Java\JDK1.7\jre\lib\javaws.jar;D:\soft\Java\JDK1.7\jre\lib\jce.jar;D:\soft\Java\JDK1.7\jre\lib\jfr.jar;D:\soft\Java\JDK1.7\jre\lib\jfxrt.jar;D:\soft\Java\JDK1.7\jre\lib\jsse.jar;D:\soft\Java\JDK1.7\jre\lib\management-agent.jar;D:\soft\Java\JDK1.7\jre\lib\plugin.jar;D:\soft\Java\JDK1.7\jre\lib\resources.jar;D:\soft\Java\JDK1.7\jre\lib\rt.jar;D:\myworkspace\02localdemo\T03TomLoader\target\classes com.zc.test.ReentrantLockVsSynchronizedTest
-------------------------------------
// 线程数1,每个线程循环 100000 次
22840888(elapse), testSynchronized: result=100000, threadCount=1, loopCount=100000
26080245(elapse), testAtomicInteger: result=100000, threadCount=1, loopCount=100000
8243398(elapse), testReentrantLockUnfair: result=100000, threadCount=1, loopCount=100000
-------------------------------------
// 线程数2,每个线程循环 100000 次
5484065(elapse), testSynchronized: result=300000, threadCount=2, loopCount=100000
5490809(elapse), testAtomicInteger: result=300000, threadCount=2, loopCount=100000
11825254(elapse), testReentrantLockUnfair: result=300000, threadCount=2, loopCount=100000
-------------------------------------
// 线程数4,每个线程循环 100000 次
11477771(elapse), testSynchronized: result=700000, threadCount=4, loopCount=100000
15624982(elapse), testReentrantLockUnfair: result=700000, threadCount=4, loopCount=100000
29080910(elapse), testAtomicInteger: result=700000, threadCount=4, loopCount=100000
-------------------------------------
// 线程数6,每个线程循环 100000 次
15473673(elapse), testSynchronized: result=1300000, threadCount=6, loopCount=100000
32343434(elapse), testAtomicInteger: result=1300000, threadCount=6, loopCount=100000
35170504(elapse), testReentrantLockUnfair: result=1300000, threadCount=6, loopCount=100000
-------------------------------------
// 线程数8,每个线程循环 100000 次
18780768(elapse), testSynchronized: result=2100000, threadCount=8, loopCount=100000
19249649(elapse), testReentrantLockUnfair: result=2100000, threadCount=8, loopCount=100000
51532676(elapse), testAtomicInteger: result=2100000, threadCount=8, loopCount=100000
-------------------------------------
// 线程数10,每个线程循环 100000 次
22142404(elapse), testReentrantLockUnfair: result=3100000, threadCount=10, loopCount=100000
28116170(elapse), testSynchronized: result=3100000, threadCount=10, loopCount=100000
46799583(elapse), testAtomicInteger: result=3100000, threadCount=10, loopCount=100000
-------------------------------------
// 线程数50,每个线程循环 100000 次
154866338(elapse), testReentrantLockUnfair: result=8100000, threadCount=50, loopCount=100000
164094417(elapse), testSynchronized: result=8100000, threadCount=50, loopCount=100000
190372008(elapse), testAtomicInteger: result=8100000, threadCount=50, loopCount=100000
-------------------------------------
// 线程数100,每个线程循环 100000 次
320082668(elapse), testSynchronized: result=18100000, threadCount=100, loopCount=100000
399773154(elapse), testAtomicInteger: result=18100000, threadCount=100, loopCount=100000
669262375(elapse), testReentrantLockUnfair: result=18100000, threadCount=100, loopCount=100000
-------------------------------------
// 线程数200,每个线程循环 100000 次
557621097(elapse), testReentrantLockUnfair: result=38100000, threadCount=200, loopCount=100000
650982745(elapse), testSynchronized: result=38100000, threadCount=200, loopCount=100000
1287148955(elapse), testAtomicInteger: result=38100000, threadCount=200, loopCount=100000
-------------------------------------
// 线程数500,每个线程循环 100000 次
1319411162(elapse), testReentrantLockUnfair: result=88100000, threadCount=500, loopCount=100000
1496867673(elapse), testSynchronized: result=88100000, threadCount=500, loopCount=100000
2878890484(elapse), testAtomicInteger: result=88100000, threadCount=500, loopCount=100000
-------------------------------------
// 线程数500,每个线程循环 10000 次
134509730(elapse), testReentrantLockUnfair: result=93100000, threadCount=500, loopCount=10000
170702742(elapse), testSynchronized: result=93100000, threadCount=500, loopCount=10000
250160106(elapse), testAtomicInteger: result=93100000, threadCount=500, loopCount=10000
-------------------------------------
// 线程数500,每个线程循环 1000 次
27824108(elapse), testReentrantLockUnfair: result=93600000, threadCount=500, loopCount=1000
40523787(elapse), testSynchronized: result=93600000, threadCount=500, loopCount=1000
40963344(elapse), testAtomicInteger: result=93600000, threadCount=500, loopCount=1000
-------------------------------------
// 线程数500,每个线程循环 100 次
24163960(elapse), testSynchronized: result=93650000, threadCount=500, loopCount=100
24996452(elapse), testAtomicInteger: result=93650000, threadCount=500, loopCount=100
35085760(elapse), testReentrantLockUnfair: result=93650000, threadCount=500, loopCount=100
-------------------------------------
// 线程数500,每个线程循环 10 次
40494463(elapse), testReentrantLockUnfair: result=93655000, threadCount=500, loopCount=10
51199269(elapse), testSynchronized: result=93655000, threadCount=500, loopCount=10
52468973(elapse), testAtomicInteger: result=93655000, threadCount=500, loopCount=10
-------------------------------------
// 线程数500,每个线程循环 1 次
44156664(elapse), testAtomicInteger: result=93655500, threadCount=500, loopCount=1
46565583(elapse), testSynchronized: result=93655500, threadCount=500, loopCount=1
144862068(elapse), testReentrantLockUnfair: result=93655500, threadCount=500, loopCount=1
-------------------------------------
// 线程数1000,每个线程循环 1 次
111706963(elapse), testReentrantLockUnfair: result=93656500, threadCount=1000, loopCount=1
156553607(elapse), testAtomicInteger: result=93656500, threadCount=1000, loopCount=1
182228017(elapse), testSynchronized: result=93656500, threadCount=1000, loopCount=1
-------------------------------------
// 线程数1500,每个线程循环 1 次
176627244(elapse), testSynchronized: result=93658000, threadCount=1500, loopCount=1
202298721(elapse), testAtomicInteger: result=93658000, threadCount=1500, loopCount=1
69693840(elapse), testReentrantLockUnfair: result=93658000, threadCount=1500, loopCount=1
-------------------------------------

Process finished with exit code 0

从数据上看,正常情况下还是 synchronized 稍微快一点的.有比 ReentrantLock(非公平)慢的时候,最多也就慢一倍这样.

JDK1.6

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
D:\soft\Java\jdk1.6.0_45\bin\java.exe "-javaagent:D:\soft\JetBrains\IntelliJ IDEA 2018.1.6\lib\idea_rt.jar=61893:D:\soft\JetBrains\IntelliJ IDEA 2018.1.6\bin" -Dfile.encoding=UTF-8 -classpath D:\soft\Java\jdk1.6.0_45\jre\lib\charsets.jar;D:\soft\Java\jdk1.6.0_45\jre\lib\deploy.jar;D:\soft\Java\jdk1.6.0_45\jre\lib\ext\dnsns.jar;D:\soft\Java\jdk1.6.0_45\jre\lib\ext\localedata.jar;D:\soft\Java\jdk1.6.0_45\jre\lib\ext\sunjce_provider.jar;D:\soft\Java\jdk1.6.0_45\jre\lib\ext\sunmscapi.jar;D:\soft\Java\jdk1.6.0_45\jre\lib\javaws.jar;D:\soft\Java\jdk1.6.0_45\jre\lib\jce.jar;D:\soft\Java\jdk1.6.0_45\jre\lib\jsse.jar;D:\soft\Java\jdk1.6.0_45\jre\lib\management-agent.jar;D:\soft\Java\jdk1.6.0_45\jre\lib\plugin.jar;D:\soft\Java\jdk1.6.0_45\jre\lib\resources.jar;D:\soft\Java\jdk1.6.0_45\jre\lib\rt.jar;D:\myworkspace\02localdemo\T03TomLoader\target\classes com.zc.test.ReentrantLockVsSynchronizedTest
-------------------------------------
// 线程数1,每个线程循环 100000 次
4767108(elapse), testAtomicInteger: result=100000, threadCount=1, loopCount=100000
5231298(elapse), testSynchronized: result=100000, threadCount=1, loopCount=100000
11269281(elapse), testReentrantLockUnfair: result=100000, threadCount=1, loopCount=100000
-------------------------------------
// 线程数2,每个线程循环 100000 次
2265820(elapse), testAtomicInteger: result=300000, threadCount=2, loopCount=100000
5181154(elapse), testSynchronized: result=300000, threadCount=2, loopCount=100000
9388771(elapse), testReentrantLockUnfair: result=300000, threadCount=2, loopCount=100000
-------------------------------------
// 线程数4,每个线程循环 100000 次
10028901(elapse), testSynchronized: result=700000, threadCount=4, loopCount=100000
28028493(elapse), testAtomicInteger: result=700000, threadCount=4, loopCount=100000
9190838(elapse), testReentrantLockUnfair: result=700000, threadCount=4, loopCount=100000
-------------------------------------
// 线程数6,每个线程循环 100000 次
15937569(elapse), testReentrantLockUnfair: result=1300000, threadCount=6, loopCount=100000
17108746(elapse), testSynchronized: result=1300000, threadCount=6, loopCount=100000
21985524(elapse), testAtomicInteger: result=1300000, threadCount=6, loopCount=100000
-------------------------------------
// 线程数8,每个线程循环 100000 次
20291510(elapse), testReentrantLockUnfair: result=2100000, threadCount=8, loopCount=100000
20961550(elapse), testSynchronized: result=2100000, threadCount=8, loopCount=100000
36662479(elapse), testAtomicInteger: result=2100000, threadCount=8, loopCount=100000
-------------------------------------
// 线程数10,每个线程循环 100000 次
21589658(elapse), testAtomicInteger: result=3100000, threadCount=10, loopCount=100000
23756071(elapse), testReentrantLockUnfair: result=3100000, threadCount=10, loopCount=100000
27164038(elapse), testSynchronized: result=3100000, threadCount=10, loopCount=100000
-------------------------------------
// 线程数50,每个线程循环 100000 次
126872447(elapse), testReentrantLockUnfair: result=8100000, threadCount=50, loopCount=100000
149717147(elapse), testSynchronized: result=8100000, threadCount=50, loopCount=100000
296352695(elapse), testAtomicInteger: result=8100000, threadCount=50, loopCount=100000
-------------------------------------
// 线程数100,每个线程循环 100000 次
240213603(elapse), testSynchronized: result=18100000, threadCount=100, loopCount=100000
246687627(elapse), testReentrantLockUnfair: result=18100000, threadCount=100, loopCount=100000
324364767(elapse), testAtomicInteger: result=18100000, threadCount=100, loopCount=100000
-------------------------------------
// 线程数200,每个线程循环 100000 次
522835023(elapse), testReentrantLockUnfair: result=38100000, threadCount=200, loopCount=100000
749915548(elapse), testSynchronized: result=38100000, threadCount=200, loopCount=100000
1348022018(elapse), testAtomicInteger: result=38100000, threadCount=200, loopCount=100000
-------------------------------------
// 线程数500,每个线程循环 100000 次
1252515069(elapse), testReentrantLockUnfair: result=88100000, threadCount=500, loopCount=100000
1796960863(elapse), testSynchronized: result=88100000, threadCount=500, loopCount=100000
3533896401(elapse), testAtomicInteger: result=88100000, threadCount=500, loopCount=100000
-------------------------------------
-------------------------------------
// 线程数500,每个线程循环 10000 次
120825372(elapse), testReentrantLockUnfair: result=93100000, threadCount=500, loopCount=10000
173094359(elapse), testSynchronized: result=93100000, threadCount=500, loopCount=10000
327061935(elapse), testAtomicInteger: result=93100000, threadCount=500, loopCount=10000
-------------------------------------
// 线程数500,每个线程循环 1000 次
25813109(elapse), testReentrantLockUnfair: result=93600000, threadCount=500, loopCount=1000
26570239(elapse), testSynchronized: result=93600000, threadCount=500, loopCount=1000
28246953(elapse), testAtomicInteger: result=93600000, threadCount=500, loopCount=1000
-------------------------------------
// 线程数500,每个线程循环 100 次
25892576(elapse), testReentrantLockUnfair: result=93650000, threadCount=500, loopCount=100
26211908(elapse), testAtomicInteger: result=93650000, threadCount=500, loopCount=100
32821991(elapse), testSynchronized: result=93650000, threadCount=500, loopCount=100
-------------------------------------
// 线程数500,每个线程循环 10 次
24638120(elapse), testAtomicInteger: result=93655000, threadCount=500, loopCount=10
28614668(elapse), testSynchronized: result=93655000, threadCount=500, loopCount=10
28632262(elapse), testReentrantLockUnfair: result=93655000, threadCount=500, loopCount=10
-------------------------------------
// 线程数500,每个线程循环 1 次
28014418(elapse), testAtomicInteger: result=93655500, threadCount=500, loopCount=1
30410727(elapse), testSynchronized: result=93655500, threadCount=500, loopCount=1
37675310(elapse), testReentrantLockUnfair: result=93655500, threadCount=500, loopCount=1
-------------------------------------
// 线程数1000,每个线程循环 1 次
49730166(elapse), testAtomicInteger: result=93656500, threadCount=1000, loopCount=1
54536567(elapse), testSynchronized: result=93656500, threadCount=1000, loopCount=1
55911835(elapse), testReentrantLockUnfair: result=93656500, threadCount=1000, loopCount=1
-------------------------------------
// 线程数1500,每个线程循环 1 次
77436982(elapse), testReentrantLockUnfair: result=93658000, threadCount=1500, loopCount=1
80245284(elapse), testSynchronized: result=93658000, threadCount=1500, loopCount=1
82252179(elapse), testAtomicInteger: result=93658000, threadCount=1500, loopCount=1
-------------------------------------

Process finished with exit code 0

从上面数据看,线程数大于等于6后 ReentrantLockUnfair 快于 synchronized,线程数小的时候 synchronized 快一些.

JDK11

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
D:\soft\Java\jdk-11.0.4\bin\java.exe -Didea.launcher.port=62514 "-Didea.launcher.bin.path=D:\soft\JetBrains\IntelliJ IDEA 2018.1.6\bin" -Dfile.encoding=UTF-8 -classpath "D:\myworkspace\02localdemo\T03TomLoader\target\classes;D:\soft\JetBrains\IntelliJ IDEA 2018.1.6\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMainV2 com.zc.test.ReentrantLockVsSynchronizedTest
-------------------------------------
// 线程数1,每个线程循环 100000 次
4207617(elapse), testAtomicInteger: result=100000, threadCount=1, loopCount=100000
7003311(elapse), testSynchronized: result=100000, threadCount=1, loopCount=100000
12754512(elapse), testReentrantLockUnfair: result=100000, threadCount=1, loopCount=100000
-------------------------------------
// 线程数2,每个线程循环 100000 次
4266557(elapse), testAtomicInteger: result=300000, threadCount=2, loopCount=100000
6544986(elapse), testSynchronized: result=300000, threadCount=2, loopCount=100000
10791310(elapse), testReentrantLockUnfair: result=300000, threadCount=2, loopCount=100000
-------------------------------------
// 线程数4,每个线程循环 100000 次
7923773(elapse), testAtomicInteger: result=700000, threadCount=4, loopCount=100000
9049499(elapse), testReentrantLockUnfair: result=700000, threadCount=4, loopCount=100000
10100450(elapse), testSynchronized: result=700000, threadCount=4, loopCount=100000
-------------------------------------
// 线程数6,每个线程循环 100000 次
13618380(elapse), testAtomicInteger: result=1300000, threadCount=6, loopCount=100000
14245901(elapse), testReentrantLockUnfair: result=1300000, threadCount=6, loopCount=100000
15905607(elapse), testSynchronized: result=1300000, threadCount=6, loopCount=100000
-------------------------------------
// 线程数8,每个线程循环 100000 次
14097525(elapse), testAtomicInteger: result=2100000, threadCount=8, loopCount=100000
14484007(elapse), testSynchronized: result=2100000, threadCount=8, loopCount=100000
20932226(elapse), testReentrantLockUnfair: result=2100000, threadCount=8, loopCount=100000
-------------------------------------
// 线程数10,每个线程循环 100000 次
9905157(elapse), testAtomicInteger: result=3100000, threadCount=10, loopCount=100000
28208539(elapse), testSynchronized: result=3100000, threadCount=10, loopCount=100000
40982698(elapse), testReentrantLockUnfair: result=3100000, threadCount=10, loopCount=100000
-------------------------------------
// 线程数50,每个线程循环 100000 次
67569652(elapse), testAtomicInteger: result=8100000, threadCount=50, loopCount=100000
119220208(elapse), testReentrantLockUnfair: result=8100000, threadCount=50, loopCount=100000
127837187(elapse), testSynchronized: result=8100000, threadCount=50, loopCount=100000
-------------------------------------
// 线程数100,每个线程循环 100000 次
155161331(elapse), testAtomicInteger: result=18100000, threadCount=100, loopCount=100000
214199043(elapse), testSynchronized: result=18100000, threadCount=100, loopCount=100000
271890809(elapse), testReentrantLockUnfair: result=18100000, threadCount=100, loopCount=100000
-------------------------------------
// 线程数200,每个线程循环 100000 次
346384305(elapse), testAtomicInteger: result=38100000, threadCount=200, loopCount=100000
465400423(elapse), testReentrantLockUnfair: result=38100000, threadCount=200, loopCount=100000
658154372(elapse), testSynchronized: result=38100000, threadCount=200, loopCount=100000
-------------------------------------
// 线程数500,每个线程循环 100000 次
930565965(elapse), testAtomicInteger: result=88100000, threadCount=500, loopCount=100000
1305456443(elapse), testReentrantLockUnfair: result=88100000, threadCount=500, loopCount=100000
1745141403(elapse), testSynchronized: result=88100000, threadCount=500, loopCount=100000
-------------------------------------
-------------------------------------
// 线程数500,每个线程循环 10000 次
88273743(elapse), testAtomicInteger: result=93100000, threadCount=500, loopCount=10000
132994002(elapse), testReentrantLockUnfair: result=93100000, threadCount=500, loopCount=10000
165907776(elapse), testSynchronized: result=93100000, threadCount=500, loopCount=10000
-------------------------------------
// 线程数500,每个线程循环 1000 次
32739299(elapse), testAtomicInteger: result=93600000, threadCount=500, loopCount=1000
34829766(elapse), testSynchronized: result=93600000, threadCount=500, loopCount=1000
37466527(elapse), testReentrantLockUnfair: result=93600000, threadCount=500, loopCount=1000
-------------------------------------
// 线程数500,每个线程循环 100 次
34564096(elapse), testReentrantLockUnfair: result=93650000, threadCount=500, loopCount=100
35123587(elapse), testAtomicInteger: result=93650000, threadCount=500, loopCount=100
40711163(elapse), testSynchronized: result=93650000, threadCount=500, loopCount=100
-------------------------------------
// 线程数500,每个线程循环 10 次
33697882(elapse), testAtomicInteger: result=93655000, threadCount=500, loopCount=10
34600750(elapse), testSynchronized: result=93655000, threadCount=500, loopCount=10
36510584(elapse), testReentrantLockUnfair: result=93655000, threadCount=500, loopCount=10
-------------------------------------
// 线程数500,每个线程循环 1 次
35484557(elapse), testAtomicInteger: result=93655500, threadCount=500, loopCount=1
35623551(elapse), testReentrantLockUnfair: result=93655500, threadCount=500, loopCount=1
36133778(elapse), testSynchronized: result=93655500, threadCount=500, loopCount=1
-------------------------------------
// 线程数1000,每个线程循环 1 次
66762085(elapse), testAtomicInteger: result=93656500, threadCount=1000, loopCount=1
67941473(elapse), testReentrantLockUnfair: result=93656500, threadCount=1000, loopCount=1
74041623(elapse), testSynchronized: result=93656500, threadCount=1000, loopCount=1
-------------------------------------
// 线程数1500,每个线程循环 1 次
98764781(elapse), testReentrantLockUnfair: result=93658000, threadCount=1500, loopCount=1
100622126(elapse), testSynchronized: result=93658000, threadCount=1500, loopCount=1
102833697(elapse), testAtomicInteger: result=93658000, threadCount=1500, loopCount=1
-------------------------------------

Process finished with exit code 0

线程数少的时候还是 synchronized 快,线程数多的时候二者基本持平.

测试使用的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package com.zc.concurrent.issue;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
// import java.util.concurrent.atomic.LongAdder;
import java.util.concurrent.atomic.LongAdder;
import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockVsSynchronizedTest {

public static AtomicInteger a = new AtomicInteger(0);

// JDK8 才有
public static LongAdder b = new LongAdder();

public static int c = 0;

public static int d = 0;

public static int e = 0;

public static final ReentrantLock fairLock = new ReentrantLock(true);

public static final ReentrantLock unfairLock = new ReentrantLock();

public static void main(String[] args) throws InterruptedException {
System.out.println("-------------------------------------");
testAll(1, 100000);

System.out.println("-------------------------------------");
testAll(2, 100000);

System.out.println("-------------------------------------");
testAll(4, 100000);

System.out.println("-------------------------------------");
testAll(6, 100000);

System.out.println("-------------------------------------");
testAll(8, 100000);

System.out.println("-------------------------------------");
testAll(10, 100000);

System.out.println("-------------------------------------");
testAll(50, 100000);

System.out.println("-------------------------------------");
testAll(100, 100000);

System.out.println("-------------------------------------");
testAll(200, 100000);

System.out.println("-------------------------------------");
testAll(500, 100000);

System.out.println("-------------------------------------");
testAll(500, 10000);

System.out.println("-------------------------------------");
testAll(500, 1000);

System.out.println("-------------------------------------");
testAll(500, 100);

System.out.println("-------------------------------------");
testAll(500, 10);


System.out.println("-------------------------------------");
testAll(500, 1);

System.out.println("-------------------------------------");
testAll(1000, 1);

System.out.println("-------------------------------------");
testAll(1500, 1);

System.out.println("-------------------------------------");
}

public static void testAll(int threadCount, int loopCount) throws InterruptedException {
testAtomicInteger(threadCount, loopCount);

testLongAdder(threadCount, loopCount);

testSynchronized(threadCount, loopCount);

testReentrantLockUnfair(threadCount, loopCount);

testReentrantLockFair(threadCount, loopCount);
}

public static void testAtomicInteger(int threadCount, int loopCount) throws InterruptedException {
long start = System.nanoTime();

CountDownLatch countDownLatch = new CountDownLatch(threadCount);
for (int i = 0; i < threadCount; i++) {
new Thread(() -> {
for (int j = 0; j < loopCount; j++) {
a.incrementAndGet();
}
countDownLatch.countDown();
}).start();
}

countDownLatch.await();

System.out.println((System.nanoTime() - start) + "(elapse), " + "testAtomicInteger: result=" + a.get() + ", threadCount=" + threadCount + ", loopCount=" + loopCount);
}

public static void testLongAdder(int threadCount, int loopCount) throws InterruptedException {
long start = System.nanoTime();

CountDownLatch countDownLatch = new CountDownLatch(threadCount);
for (int i = 0; i < threadCount; i++) {
new Thread(() -> {
for (int j = 0; j < loopCount; j++) {
b.increment();
}
countDownLatch.countDown();
}).start();
}

countDownLatch.await();

System.out.println((System.nanoTime() - start) + "(elapse), " + "testLongAdder: result=" + b.sum() + ", threadCount=" + threadCount + ", loopCount=" + loopCount);
}

public static void testReentrantLockFair(int threadCount, int loopCount) throws InterruptedException {
long start = System.nanoTime();

CountDownLatch countDownLatch = new CountDownLatch(threadCount);
for (int i = 0; i < threadCount; i++) {
new Thread(() -> {
for (int j = 0; j < loopCount; j++) {
fairLock.lock();
c++;
fairLock.unlock();
}
countDownLatch.countDown();
}).start();
}

countDownLatch.await();

System.out.println((System.nanoTime() - start) + "(elapse), " + "testReentrantLockFair: result=" + c + ", threadCount=" + threadCount + ", loopCount=" + loopCount);
}

public static void testReentrantLockUnfair(int threadCount, int loopCount) throws InterruptedException {
long start = System.nanoTime();

CountDownLatch countDownLatch = new CountDownLatch(threadCount);
for (int i = 0; i < threadCount; i++) {
new Thread(() -> {
for (int j = 0; j < loopCount; j++) {
unfairLock.lock();
d++;
unfairLock.unlock();
}
countDownLatch.countDown();
}).start();
}

countDownLatch.await();

System.out.println((System.nanoTime() - start) + "(elapse), " + "testReentrantLockUnfair: result=" + d + ", threadCount=" + threadCount + ", loopCount=" + loopCount);
}

public static void testSynchronized(int threadCount, int loopCount) throws InterruptedException {
long start = System.nanoTime();

CountDownLatch countDownLatch = new CountDownLatch(threadCount);
for (int i = 0; i < threadCount; i++) {
new Thread(() -> {
for (int j = 0; j < loopCount; j++) {
synchronized (ReentrantLockVsSynchronizedTest.class) {
e++;
}
}
countDownLatch.countDown();
}).start();
}

countDownLatch.await();

System.out.println((System.nanoTime() - start) + "(elapse), " + "testSynchronized: result=" + e + ", threadCount=" + threadCount + ", loopCount=" + loopCount);
}
}