Java中的类锁定和对象锁定之间的区别(Difference between class locking and object locking in Java)

编程入门 行业动态 更新时间:2024-10-25 20:31:30
Java中的类锁定和对象锁定之间的区别(Difference between class locking and object locking in Java)

人们讲述了两种类型的多线程锁 - 对象和类。 据我所知,锁定只在对象上完成。

案例1:使用new或工厂方法创建的对象

void synchronized myMethod(Type param) { //will lock on the instance used to call this method }

要么

synchronized(this) { //will lock on current object }

要么

synchronized(obj1) { //will lock on specified obj1 object }

情况2:关于java.lang.Class对象

这被称为类锁,并且可以与静态字段或方法或块一起使用,因为它们属于类并在所有对象和其他类属性之间共享。

static void synchronized method() { //will lock the Class object }

要么

static { synchronized(SomeClass.class){ int a = 2; } } 为什么我认为这也是一种对象锁定,因为类被加载到JVM中的方法区域中,并且该类的所有静态属性都包装在由JVM创建的java.lang.Class对象中 。 所以在抽象的背后,它的对象锁定和图片中,我们看到类锁定。 所以我也可以推断一件事。 正如由线程锁定的对象不能被另一个线程获取,只要它不被第一个线程释放,类锁定( java.lang.Class实例)也以相同的方式工作。

我想知道在同步静态方法的情况下,在以下两种情况下锁定线程获取哪个类:

该方法在定义它的同一类中调用。 此方法从具有派生类名称的派生类中调用。

这是我对这个问题的理解。 请添加或更正。

People tell about two types of multi-threaded locking - object and class. In my knowledge, locking is done on objects only.

Case 1: On objects we create using new or factory methods etc.

void synchronized myMethod(Type param) { //will lock on the instance used to call this method }

or

synchronized(this) { //will lock on current object }

or

synchronized(obj1) { //will lock on specified obj1 object }

Case 2: On java.lang.Class objects

This is called class lock, and can be used with static fields or methods or blocks, as they belong to class and shared among all the objects, and other class properties.

static void synchronized method() { //will lock the Class object }

or

static { synchronized(SomeClass.class){ int a = 2; } } Why I am thinking this also as an object locking because classes are loaded into the Method Area in the JVM, and all the static properties of the class are wrapped inside a java.lang.Class object created by JVM. So behind abstraction, its object locking and in the picture, we see Class locking. So I can also infer one more thing. Just as objects locked by a thread can not be acquired by another thread as long as it is not released by first thread, class locking (the java.lang.Class instance) also works in same manner.

I want to know in case of synchronized static methods, lock on which class is acquired by the thread in following two cases:

This method is called from same class where it is defined. This method is called from derived class with derived class name.

This is my understanding so far regarding the subject. Please add on or rectify.

最满意答案

唯一的区别是,类实例上的static synchronized锁定和实例上的非静态synchronized方法锁定。

人们讲述了两种类型的多线程锁定

有对象实例锁和Lock样式锁。 令人困惑的是, Lock具有两个。

对象和类

不正确,因为你已经制定出来了。

只是因为人们说东西并不是真的。 通常人们会说很多胡话。 事实上,有整个网站致力于关于Java的无意义。 :P

The only difference is that a static synchronized locks on the class instance and a non-static synchronized method locks on the instance.

People tell about two types of multi-threaded locking

There is object instance locks and Lock style locks. A Lock, confusingly, has both.

object and class

Not true as you have worked out already.

Just because people say stuff doesn't make it true. Often people say a lot of nonsense. In fact there is whole web sites devoted to non-sense about Java. :P

更多推荐

本文发布于:2023-07-21 09:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1209001.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:区别   对象   Java   Difference   object

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!