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/