Talk about the synchronized keyword in java

 

1.synchronized three kinds of usage

 

public class Client {
    public static void main(String[] args) {
        testSynchronized();
    }

    private static void testSynchronized() {
        new Foo().sayHello();
    }
    static class Foo {
    //修饰代码块
    void sayHello() {
        synchronized (this) {
            System.out.println("hello");
        }
    }
    //修饰示例方法
    synchronized void sayGood(){
        System.out.println("good");
    }
    //修饰静态方法
    static synchronized void sayHi(){
        System.out.println("hi");
    }
  } 
}

  

(1) Modified example of the method, the current acting on the lock object instance, before entering the synchronization code to obtain the current instance of the object lock
(2) modifying the static method, the lock acting on the current class object, before entering the synchronization code to obtain the current class object locks. That is to lock the current class, will make
all object instances for the class, because static members do not belong to any instance of an object, the class members (static indicates that this is a static class
resources, no matter how many new objects, only one, so all objects of that class have added lock). So if a thread A call to a real
non-static synchronized method according to the object, and the thread needs to call the B instance object static synchronized method belongs to the class, it is allowed
promised, mutual exclusion will not happen, because access static synchronized method takes lock is a lock current class, and access to non-static
synchronized method lock is occupied by the current instance of the object lock.
(3) modifying code blocks, designated lock object, for a given object locking into the lock before the synchronization code library to obtain a given object. And synchronized square
method as, synchronized (this) is a locking block of the current object. synchronized keyword static methods applied static and
synchronized (class) is the code block is locked to the class Class. Here mention about: synchronized non-static static keyword added to
the method is to lock an object instance.

The underlying principle 2.synchronized

synchronized keyword underlying principle belong JVM level. View Foo class by JDK comes javap command byte code related information: javap -v Client $ Foo.class

 

sync block synchronized statement to achieve using the monitorenter and monitorexit instructions, which point to the same monitorenter instruction
start position of the synchronization code block, monitorexit instruction indicating the end position of the synchronization code block. When performing monitorenter instruction thread tries to
acquire the lock is acquired monitor (monitor object exists in the subject header of each Java object, synchronized lock is acquired in this way
locks, it is why any object in Java can be used as a lock reason) the holder of the right. when the counter is 0 can succeed, will acquire the lock counter set
to 1 is incremented. After performing the appropriate monitorexit instruction, the lock counter is set to 0, indicating that the lock is released. If you get the object lock failed, when
the current thread will block until another thread lock is released.

Situation synchronized modification of the method

 

synchronized modification of the method does not monitorenter instructions and monitorexit command, made on behalf of the really
ACC_SYNCHRONIZED identifier, which indicates that the method is a synchronous method, JVM through the ACC_SYNCHRONIZED access flag to
identify if a method is declared as synchronization method to perform corresponding synchronous call.

Guess you like

Origin www.cnblogs.com/strongmore/p/11285977.html