Java同步和重新进入锁定 [英] Java Synchronization and Re-Entrant locking
问题描述
当我们在对象上同步时,这是重新进入锁吗?同步锁和重入锁之间是否有真正的区别?
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 ?
亲切的问候,
推荐答案
是的,通过synchronized
关键字锁定是可重新输入的.但是,它们之间的实现可以有所不同.例如,在早期版本的JVM中,ReentrantLock
的实现具有比synchronized
关键字更好的吞吐量.实现是否不同或如何不同取决于JVM实现/版本.
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.
通常,如果您不需要类ReentrantLock
提供的其他功能,通常我建议使用synchronized
关键字.但这最终是一种偏爱.
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.
这篇关于Java同步和重新进入锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!