synchronized block vs ReentantLock performance

Java synchronized block vs ReentantLock performance

Result summary

Result (100000 run, 10 thread)

Time [ms]
Without synchronization 282
Synchronized block 7453
ReentrantLock lock 516
ReentantReadWriteLock read lock 344
ReentantReadWriteLock write lock 516

Conclusion: The java new locking mechanism is significant faster than old synchronized block strategy, in the result you can see the read/write lock benefits, because it's not block the other threads.

ps: In the test, we only get one item from HashMap, and after that we call Thread.yield (emulate the running thread changed before unlock the resource).

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License