Java Synchronization and Re-Entrant locking

后端 未结 1 835
广开言路
广开言路 2021-01-16 15:11

when we Synchronize on an object, is this a Re-Entrant lock? Is there a real difference between between a Synchronization lock and a Re-Entrant lock ?

Kind Regards,

相关标签:
1条回答
  • 2021-01-16 16:01

    Yes, lock by synchronized keyword is re-entrant. The implementation can be different between them though. For example, in earlier versions of JVM, the ReentrantLock's implementation had much better through-put than synchronized keyword had. If or how the implementation differs is dependent on the JVM implementation/version.

    In general, I tend to recommend to use the synchronized keyword if you don't require additional features the class ReentrantLock provides. But this is ultimately a preference.

    0 讨论(0)
提交回复
热议问题