What happens when a static synchronized method is called by two threads using different instances at the same time? Is it possible? The object lock is used for non static synchronized method but what type lock is used for static synchronized method?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
It is possible.
The threads lock on the
Class
object of the class, like onMyClass.class
.See JLS, Section 8.4.3.6. synchronized Methods:
It is the same as synchronizing on the
Class
object implementing the method, so yes, it is possible, and yes, the mechanism effectively ignores the instance fro which the method is called:is a shortcut for writing this:
static synchronized methods use locks on instance of type java.lang.Class. That is, each accessible class is represented by an object of type Class in runtime, and that object is used by static synchronized methods.
When using a static lock the objects are ignored. The lock is acquired on class rather than objects.