Java同步和重新进入锁定 [英] Java Synchronization and Re-Entrant locking

查看:88
本文介绍了Java同步和重新进入锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们在对象上同步时,这是重新进入锁吗?同步锁和重入锁之间是否有真正的区别?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆