Home  >  Article  >  Java  >  Introduction to the four methods used by synchronized

Introduction to the four methods used by synchronized

不言
不言forward
2018-10-08 15:16:523300browse

This article brings you an introduction to the four methods of using synchronized. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Modify a method
synchronized Modifying a method is very simple, that is, add synchronized in front of the method, for example:

public synchronized void method()
{
   // todo
}

cannot be used when defining interface methods Use the synchronized keyword.
The constructor cannot use the synchronized keyword, but can use the synchronized code block for synchronization.
The synchronized keyword cannot be inherited. If you want to synchronize, you need to add the keyword explicitly.
synchronized The method modified by the keyword is not synchronized by default if it is overridden. If you want to synchronize, you need to add the keyword explicitly, or the method of the super parent class is equivalent to synchronization.

2. Modify code block

public  void method()
{
   synchronized(this)
   synchronized(XX.class)
}

synchronized(this) locks the current object. If there are several objects currently, then there are multiple copies of this. Here is this Only the same object can be locked.
synchronized(XX.class) This lock is useful as long as it is a class of this type.

When two concurrent threads access the synchronized(this) synchronization code block in the same object object, one Only one thread can be executed at a time. Another thread must wait for the current thread to finish executing this code block before it can execute this code block. When a thread accesses a synchronized(this) synchronized code block of an object, another thread can still access the non-synchronized(this) synchronized code block in the object. What is particularly critical is that when a thread accesses a synchronized(this) synchronized code block of object, other threads' access to all other synchronized(this) synchronized code blocks in object will be blocked.
The third example also applies to other synchronized code blocks. That is to say, when a thread accesses a synchronized(this) synchronized code block of object, it obtains the object lock of this object. As a result, other threads' access to all synchronized code parts of the object object is temporarily blocked.
The above rules are also applicable to other object locks.

3. Modify static methods
We know that static methods belong to classes and not to objects. Similarly, the static method modified by synchronized locks all objects of this class, and all classes will have a locking effect when using it

public synchronized static void method() {
   // todo
   }

4. Modify a class
What it does The scope is the part enclosed in parentheses after synchronized, and the objects it affects are all objects of this class. As long as it is a class of this type, it will work no matter how many objects there are. The following code

class ClassName {
    public void method() {
       synchronized(ClassName.class) {
                 // todo       
                 }
    }
 }

summary:

A. Regardless of whether the synchronized keyword is added to a method or an object, if the object it acts on is non-static, the lock it acquires is the object; if synchronized If the object is a static method or a class, the lock it acquires is the same lock for all objects of the class.
B. Each object has only one lock associated with it. Whoever gets this lock can run the code it controls.
C. Achieving synchronization requires a lot of system overhead and may even cause deadlock, so try to avoid unnecessary synchronization control.
D. The synchronization keyword locks the object

The above is the detailed content of Introduction to the four methods used by synchronized. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete