Java uses synchronized to implement built-in locks


Multiple processes or threads accessing the same resource at the same time (or in the same period of time) will cause concurrency (thread safety) problems. Locks can be used to solve concurrency problems. synchronizedThe built-in lock realized by keywords can be used directly in the stand-alone case .

Each Javaobject can be used as a lock to achieve synchronization. These locks are called built-in locks. When a thread enters a synchronized code block or method, it will automatically acquire the lock, and when it exits a synchronized code block or method, it will release the lock. The only way to obtain a built-in lock is to enter the synchronized code block or method protected by this lock. JavaThe built-in lock is a mutex lock, which means that at most only one thread can acquire the lock. When the thread Atries to acquire Bthe built-in lock held by the thread, the thread Amust wait or block until the thread Breleases the lock. If the thread Bdoes not release With this lock, the thread Awill wait forever.

JavaThe built-in locks can be basically divided into object locks and class locks . Object locks are used for object instance methods or object instances, and class locks are used for static methods of classes or classobjects of classes . We know that there can be many instance objects of a class, but each class has only one classobject, so the object locks of different object instances do not interfere with each other, but each class has only one class lock. One thing that must be noted is that, in fact, the class lock is only a conceptual thing, not real, it is only used to help us understand the difference between locking instance methods and static methods .

synchronizedIt is just a locking mechanism for built-in locks. When a synchronizedkeyword is added to a method , it indicates that the built-in lock must be acquired before it can be executed, and it cannot prevent other threads from accessing methods that do not need to acquire the built-in lock.

1. Object lock

1.1. Synchronization method

The synchronization method is to lock the object, and other threads will not be able to access the methods and code blocks that need to acquire the object lock .

public class Test{
    
    
    
    public synchronized void print(){
    
        
        System.out.println("hello world!");
    }
    
}

The above code indicates that the access print()method needs to acquire Testthe lock of the instance object.

1.2. Synchronization code block (current class object lock)

Synchronization code block, this way of writing also locks the instance object of this class, and the 1.1.effect is the same, other threads will not be able to access the method and code block that need to acquire the object lock .

public class Test{
    
    
    
    public void print(){
    
        
        synchronized(this){
    
    
            System.out.println("hello world!");
        }
    }
    
}

The above code indicates print()that the code block in the access method needs to acquire Testthe lock of the instance object.

1.3. Synchronization code block (passing in class object lock)

Synchronous code block, this way of writing locks the incoming object, and does not affect the access to the method and code block that need to acquire the current class object lock.

public class Test{
    
    
    
    private byte[] lock = new byte[0];
    
    public void print(){
    
        
        synchronized(lock){
    
    
            System.out.println("hello world!");
        }
    }
    
    public synchronized void print1(){
    
    
        System.out.println("123456");
    }
    
}

print()The synchronized code block in the execution method will lock the byte[]object lock. Note that Testthe object is not locked, which means that Testother synchronizedmethods and code blocks of the object will not print()be locked because of it. print1()The method can be accessed normally.

2. Class lock

The effect of the modification method and code block of the class lock is the same as that of the object lock, because the class lock is only an abstract concept, and the class lock and the object lock do not interfere with each other. Similarly, when a thread obtains an object lock, it can also obtain this type of lock, that is, obtain two locks at the same time, which is allowed.

2.1. Synchronous static methods

Because static methods are shared by all object instances, and synchronizedthe lock corresponding to the modified static method is also unique, a class lock is abstracted. Other threads will not be able to access need to get the class lock methods and code blocks.

public class Test{
    
    
    
    public synchronized static void print(){
    
        
        System.out.println("hello world!");
    }
    
}

2.2. Synchronous code block (current class lock)

This way of writing directly locks the classobjects of the current class , because each class has only one classobject, which 2.1.has the same effect. Other threads will not be able to access need to get the class lock methods and code blocks.

public class Test{
    
    
    
    public void print(){
    
        
        synchronized(Test.class){
    
    
            System.out.println("hello world!");
        }
    }
    
}

Guess you like

Origin blog.csdn.net/wb1046329430/article/details/115259802