Object Lock (Method Lock), which is for an object that declares only one memory location of the object that identifies whether the object has a lock, all it locks the current object, and generally an object lock is a synchronized modification of a non-static member variable. or a non-static member method is synchronized, for object locks, different objects access the same synchronized modified method will not block
A class lock locks the entire class, and when there are multiple threads declaring the object of the class, it will be blocked until the object that owns the class lock is destroyed or the class lock is released voluntarily, and this time the blocked thread is singled out for an object that holds the class lock and declares the class. Other threads continue to be blocked.
Whether it is a class lock or an object lock, there is no direct relationship between the parent class and the child class for blocking. When a class lock is added to a parent class, the subclass is not affected, and the opposite is true. Because the Synchronized keyword is not part of the method signature, it modifies the method. The synchronized modifier is not automatically inherited when subclasses overwrite the synchronization method in the parent class or the synchronous method declared in the interface, so the corresponding blocking problem does not occur.
Note: The blocking problem here is that it should be blocked under normal circumstances, because synchronized is not blocked because it is not transitive between the parent and child classes. That's what normally happens to be blocked, which is described in more detail below. However, when a subclass does not overwrite a method of the parent class, it is blocked by the subclass access method.
Insert sentence: The construction method cannot be truly synchronous (although you can use synchronous blocks in the constructor method). Here's how to declare an object lock and how to declare a class lock:
void2 synchronized(This 3 //code 4 56/ *isequvilantto*/voidsynchronized 9 //code10}
When the same object accesses a method in thread 1, and then accesses another lock method in thread 2, it is also blocked.
For class locks, the entire class is locked, and there can be only one object that has the lock of the current class. When an object has a class lock, another object will be blocked if it wants to compete for a lock. Two objects A A, B, if a is accessing a method function that is decorated with a class lock, then B. is not accessible. Because a class lock can only be owned by an object at the same time. is different relative to the object lock. Or a A-B two object, if a is accessing the function of the object lock modifier, then this is the time at which it can be accessed.
For object locks, when an object has a lock and accesses a method that adds an object lock, which in turn calls the other methods in the class that add an object lock, the time is not blocked. This is implemented by Java through a reentrant lock mechanism. A reentrant lock means that when an object has an object lock, the lock can be acquired repeatedly. Because synchronized blocks are reentrant, when you access an object lock method, it is not blocked to continue accessing other object locks in the method.
Method locks, Object locks, class locks