java interview -synchronized underlying implementation mechanism

A, synchronized three application modes

1, a modified example of the method, the current instance of the object is locked, in the synchronization code to be obtained before the current instance of the lock

/ ** 
 * synchronized modifying an instance method, the current lock thread is an instance of an object accountingSync 
 * When a thread is accessing an object's synchronized instance method, then the other synchronized methods other threads can not access the object 
 * Only one lock an object 
 * / 
class AccountingSync the implements the Runnable {public 

    static AccountingSync accountingSync new new AccountingSync = (); 
    // shared resource 
    static int I = 0; 
    static int J = 0; 

    public void the synchronized Increase () { 
        I ++; 
    } 

    @Override 
    public void RUN () { 
        for (int I = 0; I <1000000; I ++) { 
            the synchronized (the this) { 
                Increase (); 
            } 
        } 
    }


    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(accountingSync);
        Thread thread2 = new Thread(accountingSync);
        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();
        System.out.println(i);
    }
}  
/ ** 
 synchronized access method * Thread1 instance object obj1, the instance of the object obj1 Thread2 access the synchronized method 
 * This is allowed, because the object lock two examples are not the same. 
 * At this time, if two threads operate non-shared data, the security thread is guaranteed, if the data is shared, the thread can not guarantee safe 
 * 
 * / 
public class AccountingSyncBad the implements the Runnable { 

    static int I = 0; 

    public void the synchronized Increase () { 
        I ++; 
    } 

    @override 
    public void RUN () { 
        for (int I = 0; I <1000000; I ++) { 
            Increase (); 
        } 
    } 


    public static void main (String [] args) throws InterruptedException { 
        // new instance of the new new 
        Thread thread1 = new Thread (new AccountingSyncBad ()); 
        // new new instance of a new
        Thread thread2 = new Thread(new AccountingSyncBad());
        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();
        System.out.println(i);
    }
}   

2, modify the static method, the object lock is the class of the current class, before entering the lock synchronization code to obtain the current class object 

class AccountingSyncClass the implements the Runnable {public 

    static int I = 0; 

    / ** 
     * acting on the synchronized static method, the lock current class object 
     * / 
    public static void the synchronized Increase () { 
        I ++; 
    } 

    / ** 
     * increase4Obj method is an instance method, its object lock is the current instance of the object, 
     * if the other thread calls this method will not produce mutual exclusion, after all, different lock objects, 
     * but we should be aware may find that thread-safety issues in this case (operation sharing static variable i). 
     * / 
    Public void increase4Obj the synchronized () { 
        I ++; 
    } 

    @Override 
    public void RUN () { 
        for (int I = 0; I <1000000; I ++) { 
            Increase (); 
// increase4Obj (); 
        } 
    }


    public static void main(String[] args) throws InterruptedException{
        //new新实例
        Thread thread1 = new Thread(new AccountingSyncClass());
        //new新实例
        Thread thread2 = new Thread(new AccountingSyncClass());
        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();
        System.out.println(i);
    }
}  

3, the modified code blocks

synchronized (this) lock the current instance of the object,
the synchronized (AccountingSync.class) class lock is subject

Two, synchronized code blocks underlying principle

synchronized code block is achieved by a pair of monitorenter and monitorexit instructions, Monitor objects are basically synchronized unit.

Modern java virtual machine is optimized for sychronized introduced skew lock, lock lightweight, heavyweight lock

 

Three, java virtual machine is optimized for Synchronized

JVM optimization mechanism synchronized operation, the JVM detects when a different competition, automatically switches to the appropriate locks for

1, occurs when there is no competition, it will default to deflect the lock . CAS operation using the JVM, Word thread ID portion is provided at the head Mark objects to represent the object toward the current thread , it does not involve real mutex. To do so is based on the assumption that in many application scenarios, most of the objects will be up to the life cycle of a thread locking, use the lock deflection can reduce costs without competition.

2, when competition occurs, when there is another thread tries to lock an already locked skew lock object, jvm will revoke revoke skewed lock and switch to lightweight lock. Lightweight lock dependence CAS operations Mark Word to try to acquire the lock, if successful, locks on the use of lightweight, not heavyweight otherwise continue to escalate locks

PS: Lock downgrade also exist, when the JVM into the SafePoint security point checks for idle Monitor, and then attempt to downgrade.

Guess you like

Origin www.cnblogs.com/wjh123/p/11408571.html