.class 的 Java 同步块

发布于 2024-08-17 08:07:42 字数 294 浏览 7 评论 0原文

这段java代码是什么意思?它会获得 MyClass 的所有对象的锁定吗?

synchronized(MyClass.class) {
   //is all objects of MyClass are thread-safe now ??
}

上述代码与此代码有何不同:

synchronized(this) {
   //is all objects of MyClass are thread-safe now ??
}

What does this java code mean? Will it gain lock on all objects of MyClass?

synchronized(MyClass.class) {
   //is all objects of MyClass are thread-safe now ??
}

And how the above code differs from this one:

synchronized(this) {
   //is all objects of MyClass are thread-safe now ??
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

随风而去 2024-08-24 08:07:42

代码片段 synchronized(X.class) 使用类实例作为监视器。由于只有一个类实例(运行时表示类元数据的对象),因此该块中可以有一个线程。

使用synchronized(this)该块由实例保护。对于每个实例,只有一个线程可以进入该块。

synchronized(X.class) 用于确保块中只有一个线程。 synchronized(this) 确保每个实例只有一个线程。这是否使块中的实际代码成为线程安全取决于实现。如果仅改变实例 synchronized(this) 的状态就足够了。

The snippet synchronized(X.class) uses the class instance as a monitor. As there is only one class instance (the object representing the class metadata at runtime) one thread can be in this block.

With synchronized(this) the block is guarded by the instance. For every instance only one thread may enter the block.

synchronized(X.class) is used to make sure that there is exactly one Thread in the block. synchronized(this) ensures that there is exactly one thread per instance. If this makes the actual code in the block thread-safe depends on the implementation. If mutate only state of the instance synchronized(this) is enough.

不必你懂 2024-08-24 08:07:42

添加到其他答案:

static void myMethod() {
  synchronized(MyClass.class) {
    //code
  }
}

相当于

static synchronized void myMethod() {
  //code
}

并且

void myMethod() {
  synchronized(this) {
    //code
  }
}

相当于

synchronized void myMethod() {
  //code
}

To add to the other answers:

static void myMethod() {
  synchronized(MyClass.class) {
    //code
  }
}

is equivalent to

static synchronized void myMethod() {
  //code
}

and

void myMethod() {
  synchronized(this) {
    //code
  }
}

is equivalent to

synchronized void myMethod() {
  //code
}
月下客 2024-08-24 08:07:42

不,第一个将获得 MyClass 的类定义的锁定,而不是它的所有实例。但是,如果在实例中使用,这将有效地阻止所有其他实例,因为它们共享单个类定义。

第二个将仅获得当前实例的锁定。

至于这是否使您的对象线程安全,这是一个更加复杂的问题 - 我们需要查看您的代码!

No, the first will get a lock on the class definition of MyClass, not all instances of it. However, if used in an instance, this will effectively block all other instances, since they share a single class definition.

The second will get a lock on the current instance only.

As to whether this makes your objects thread safe, that is a far more complex question - we'd need to see your code!

絕版丫頭 2024-08-24 08:07:42

是的,它会(在任何同步块/函数上)。

我自己想知道这个问题几天了(实际上是在 kotlin 中)。我终于找到了很好的解释并想分享它:

类级别锁可防止多个线程在运行时进入该类的任何可用实例中的同步块。这意味着如果在运行时有 100 个 DemoClass 实例,那么一次只有一个线程能够在任一实例中执行 demoMethod(),而所有其他实例将被其他线程锁定。

类级别应始终进行锁定以使静态数据线程安全。我们知道 static 关键字将方法的数据与类级别关联,因此在静态字段或方法上使用锁定以使其在类级别上。

另外要注意为什么.class。这是因为 .class 相当于类的任何静态变量,类似于:

private final static Object lock = new Object();

其中锁变量名称为 class,类型为 Class>

阅读更多:
https://howtodoinjava.com/java/multi-threading /对象与类级别锁定/

Yes it will (on any synchronized block/function).

I was wondering about this question for couple days for myself (actually in kotlin). I finally found good explanation and want to share it:

Class level lock prevents multiple threads to enter in synchronized block in any of all available instances of the class on runtime. This means if in runtime there are 100 instances of DemoClass, then only one thread will be able to execute demoMethod() in any one of instance at a time, and all other instances will be locked for other threads.

Class level locking should always be done to make static data thread safe. As we know that static keyword associate data of methods to class level, so use locking at static fields or methods to make it on class level.

Plus to notice why .class. It is just because .class is equivalent to any static variable of class similar to:

private final static Object lock = new Object();

where lock variable name is class and type is Class<T>

Read more:
https://howtodoinjava.com/java/multi-threading/object-vs-class-level-locking/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文