Java 同步静态方法 : lock on object or class

标签 java class static methods synchronized

Java 文档说:

It is not possible for two invocations of synchronized methods on the same object to interleave.

这对静态方法意味着什么?由于静态方法没有关联对象,synchronized关键字会锁定类而不是对象吗?

最佳答案

为了给 Oscar 的回答添加一点细节(令人愉悦的简洁!),Java 语言规范的相关部分是 8.4.3.6, 'synchronized Methods' :

A synchronized method acquires a monitor (§17.1) before it executes. For a class (static) method, the monitor associated with the Class object for the method's class is used. For an instance method, the monitor associated with this (the object for which the method was invoked) is used.

关于Java 同步静态方法 : lock on object or class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/437620/

相关文章:

java - MIDI 乐器列表?

java - 如何在 Restful Java 服务中满足无效返回警告

python - 如何用时序图展示一个类和另一个以类的实例作为输入的类的关系?

c++ - 静态库已链接但仍 undefined reference

c# - 非静态类中的静态方法和静态类中的静态方法有什么区别?

java - 如何编写自定义约束来检查类中至少有一个 boolean 字段为 true

java - 未经检查的转换警告与对泛型类型的引用

c++ - 错误 "c does not name a type"

Java动态类加载器

c++ - 为什么当它作为静态成员变量出现时没有调用c++构造函数?