什么是类级别,对象级别,显式和内部锁定? [英] What are class level, object level, explicit and intrinsic locking?

查看:310
本文介绍了什么是类级别,对象级别,显式和内部锁定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究Java多线程概念。我经历的越多,我就越困惑。

I have been going through Java multi-threading concepts. The more I go through them, the more confused I become.

现在我不了解Java中类级别,对象级别,显式锁定和内部锁定之间的差异。有人可以让我知道这是什么?另外,如果我能得到一些例子来理解,这对我来说非常有帮助。

Right now I am not understanding the differences between class level, object level, explicit and intrinsic locking in Java. Can someone please let me know which is what? Also, if I can get some examples to understand, that will be very helpful for me.

推荐答案

显式vs内在



当您在对象上使用 synchronized 或间接作为方法签名的一部分时,您正在创建内在锁定。您依赖于与所有对象和类关联的内置锁。

Explicit vs Intrinsic

When you use synchronized on an object or indirectly as part of a method signature you are creating an intrinsic lock. You rely upon the in-built lock associated with all objects and classes.

在包含 java的Java 5+中提供了显式锁定。 util.concurrent.locks 。最常用的类可能是 ReentrantLock 。这些提供了使用内部锁定的替代方法,并提供了内部锁定无法实现的功能。

An explicit lock is provided in Java 5+ in the package java.util.concurrent.locks. The most commonly used class is probably ReentrantLock. These provide alternatives to using the intrinsic locks and offer features that are not possible with intrinsic locks.

此区别仅适用于内部锁。如果您有同步静态方法,则使用的内部锁定将与类对象本身相关联。如果在对象实例上进行同步(或具有同步的实例方法),则它将是对象级锁定。

This distinction applies to intrinsic locks only. If you have a synchronized static method, the intrinsic lock used will be associated with the class object itself. If you synchronize on an object instance (or have a synchronized instance method) it will be an object-level lock.


Brian Goetz的 Java Concurrency in Practice 是一本很好的书,用于理解Java中多线程编程的混乱世界。

Brian Goetz's Java Concurrency in Practice is an excellent book for understanding the nightmarishly confusing world of multi-threaded programming in Java.

这篇关于什么是类级别,对象级别,显式和内部锁定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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