Synchronized reentrant lock Analysis

Reentrant lock known as recursive lock means in the same thread acquires the lock of the method when the outer layer, inner layer method of re-entering the thread automatically acquire the lock (provided that the lock must be the same object or an object class),

Will not have previously been acquired yet Jitsukata and blocking occur. That same thread can perform multiple methods to hold the same lock.

First to a piece of code:

public class ReentrantSynchronized {
    public synchronized void firstIPhoneByElectric() {
        System.out.println("第一部iphone充电..." + Thread.currentThread().getName());
        secondIPhoneByElectric();
    }

    public synchronized void secondIPhoneByElectric() {
        System.out.println("第二部iphone充电..." + Thread.currentThread().getName());
    }

    public static void main(String[] args) {
        ReentrantSynchronized reentrantSynchronized = new ReentrantSynchronized();
        reentrantSynchronized.firstIPhoneByElectric();
    }

}

At the same time as a multi-use charging cable can simultaneously to multiple iphone charging, but will not be any conflict and obstruction, it is the first iphone can get charging cable (lock) can also be charged the second unit also can get charging cable iphone (lock) is charged.

Execution results are as follows:

 

 

 In the above code, the two methods in a class are built locked synchronized modification to the phone while charging the first portion (firstIPhoneByElectric process) to charge the second phone (call secondIPhoneByElectric method).

Because (the same thread may be obtained directly when calling secondIPhoneByElectric multi-use charging line (synchronized is reentrant), so the same person can be charged directly with the use of rechargeable line when a second phone charging

Current lock object, the process proceeds secondIPhoneByElectric operation).

Idea: The charging cable is (if not synchronized reentrant), you must wait until the first phone is completely charged when charging to the second phone can be used alone (in the current thread calls secondIPhoneByElectric released after charging cable

Must be freed before firstIPhoneByElectric lock acquisition), in fact, the charging line has been occupied by the first mobile phone, and can not be released, (the object lock is held by the current thread, and can not be released), it appears dead lock.

What is the principle of it?

When a thread attempts to acquire a lock, the lock reentrant first attempt to obtain and update the status value, if the status == 0 indicates that no other thread synchronization code execution, put the status is set to 1, the current thread begins execution. If the status! = 0,

It is determined whether the current thread is the thread acquired the lock, and if so execution status + 1, and the current thread can acquire the lock again. And not reentrant lock is a direct attempt to obtain and update the status of the current value, if the status! = 0

It will lead to its failure to acquire the lock, the current thread blocked.

When the lock is released, the same reentrant lock to get the current value of the status of the current thread is the premise of the thread holding the lock. If the status-1 == 0, then the current thread acquires the lock repeat all operations have been finished,

Then the thread will actually release the lock. And not reentrant lock is in determining the current thread after thread holding the lock is directly status is set to 0, the lock is released.

 

Guess you like

Origin www.cnblogs.com/java-spring/p/11532109.html