This is the 12th day of my participation in the August More Text Challenge. For details, see:August is more challenging
This article is from chaoCode. Please send a personal message and link to the author and the original address at the beginning of the article.
Violators, the author reserves the right to pursue.
preface
We usually use the synchronized keyword to modify methods or code blocks for thread-safety purposes when we operate on shared data. Synchronized: Synchronized
Column recommendation: Concurrent Programming column
The use of synchronized
The use of synchronized can be used to modify code blocks, object methods, and static methods. However, the essence of these decorations is that the lock resources are different, one is an object, one is a class.
Decorated code block
Error code :this, object lock When we use different instance objects to call the method, we will find that the result is not 20000 as we thought, because this refers to the current object, and because they are two different objects, we can not lock using this. Take a look at the results:Only if the same object is used. It is not recommended to use this, but to use a fixed object as a lock.
Execution result of the same instance object:
Let’s use an instantiated object as the object lock. (useSynchronized)
Using a different object to call it is not a problem, because the object lock is an instantiated object useSynchronized as the object lockCorrect execution result:
Methods of decorating objects
Synchronized modifies object methods. This is just like using object locks in code blocks, because synchronized modifies object methods, so it is essentially equivalent to using this in code blocks, and since a class can have many instance objects, this cannot be locked.
There is a problem with execution result, failed to lock:
Modified static methods
Synchronized modifies static methods in the same way that class locks are used in code blocks. Synchronized modifies static methods in the same way that class locks are used in code blocks. Synchronized essentially equals the class of the current class that is used in the code block. Class locking is unique so that multiple threads do not execute methods simultaneously.The result of execution is as we expected 20000:
To summarize:
Synchronized methods cannot initiate any control if they are called by a different object. When synchronized modifies a block of code, if the resource is locked using this, it is the same as the synchronized modifies object method, because a class can have multiple instance objects, each different, which is like a door with multiple keys that anyone can enter. So when we use synchronized to modify a block of code, we need to declare a unique key, such as our code above, to create a static instance object and use it as the unique key to the door, ensuring that multiple threads do not enter the method at the same time.
Synchronized uses the class of a given class, and it implements uniqueness, regardless of how many instances of that class there are. So you can do multi-threading and share resources, and it’s not a problem. So it would be pretty straightforward if we used a class lock when we were modifying code blocks, unique class locks, just like we do with static methods.
Thank you for watching, and feel free to let us know in the comments section if you have any mistakes. If this post has helped you, feel free to like it at 👍.