synchronized 與 Lock 性能比較

synchronized 與 Lock 性能比較

JDK1.8

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

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

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

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 快,線程數多的時候二者基本持平.

測試使用的代碼

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);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章