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,
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.