The previous one locks the instance object, locking the current object. If there are multiple instance objects, these synchronizedmethods are not synchronized. The second type of lock is a class object. There is only one class object, so it is synchronized.
As shown in the above code, the test1 method is equivalent to test2. When this is the same object, blocking will occur. Of course, it doesn't matter if they are different objects, because this is different. It's called an object-level lock. test3 is equivalent to test4. Here, the class object is used as the lock, because generally there is only one class instance of a class, so it will be locked every time you enter this method. It's called a class-level lock.
The lock used by the non-static synchronization method (A) is the current instance object itself. After A of one instance acquires the lock, other A’s of the instance must wait for the lock to be released. Multiple instances use different locks;
The lock used by the static synchronization method (B) is the class object itself. Once one B acquires the lock, other B must wait to release the lock, whether it is one instance or multiple instances;
In addition, different locks are used between A and B, so there will be no competition;
You are talking about the concept of "mutex lock". There are two situations for the synchronized modification method:
【Non-static method】
When a method is modified by synchronized, the lock object is the object to which the current method belongs, which is this in the method.
【Static method】
When a static method is modified by synchronized, the object locked by the static method is the current class object (an instance of the Class class). Each class has a unique class object. How to obtain a class object: classname.class.
For mutually exclusive scenarios, two points need to be understood:
1. Static methods and non-static methods are declared synchronized at the same time, and there is a non-mutually exclusive relationship between them. The reason is that static methods lock the class object rather than the object to which the current method belongs.
2. When Synchronized modifies two different pieces of code, but the lock objects are the same, the two threads are mutually exclusive when they call the two pieces of code respectively
So when you say "only one thread can call one of the functions at a time" (i.e. mutual exclusion), the judgment condition is whether the lock objects are the same, regardless of the method type.
For synchronized modification of object methods, the lock is the object itself, which is this; For synchronized modification of static methods, the lock is the Class object itself, which is the class object created by the class loader;
The previous one locks the instance object, locking the current object. If there are multiple instance objects, these
synchronized
methods are not synchronized. The second type of lock is a class object. There is only one class object, so it is synchronized.As shown in the above code, the test1 method is equivalent to test2. When this is the same object, blocking will occur. Of course, it doesn't matter if they are different objects, because this is different. It's called an object-level lock.
test3 is equivalent to test4. Here, the class object is used as the lock, because generally there is only one class instance of a class, so it will be locked every time you enter this method. It's called a class-level lock.
The lock used by the non-static synchronization method (A) is the current instance object itself. After A of one instance acquires the lock, other A’s of the instance must wait for the lock to be released. Multiple instances use different locks;
The lock used by the static synchronization method (B) is the class object itself. Once one B acquires the lock, other B must wait to release the lock, whether it is one instance or multiple instances;
In addition, different locks are used between A and B, so there will be no competition;
Class
When a class creates an object, it represents a common class. At this time, the "class lock" is the lock on this instance objectYou are talking about the concept of "mutex lock". There are two situations for the synchronized modification method:
【Non-static method】
【Static method】
For mutually exclusive scenarios, two points need to be understood:
So when you say "only one thread can call one of the functions at a time" (i.e. mutual exclusion), the judgment condition is whether the lock objects are the same, regardless of the method type.
For synchronized modification of object methods, the lock is the object itself, which is this;
For synchronized modification of static methods, the lock is the Class object itself, which is the class object created by the class loader;