“This is the 22nd day of my participation in the Gwen Challenge.
AQS:
LockSupport: Underlying AQS support
JUC–>AQS–>LockSupport
1. Reentrant lock :(recursive lock)
java.util.concurrent,atomic.Lock;
ReentrLock;
Copy the code
Reentrant lock – is a process in which the same thread can automatically enter the outer lock object if the inner lock object is the same as the outer lock object. (it will not block if the lock has not been released before)
The logic of a general lock is that the data to be changed is divided into detailed processes,
2. Parse lock usage process:
First two objects to, go home directly, a first to get to the key, enter directly, but this atomic operation, when a get to lock objects, B can only wait outside, wait until a object lock (released after, mean to unlock,) and B object can be used, it is a process of simply lock object; The process of releasing from preemption; Multiple processes in a thread can acquire the same lock, holding the synchronization lock can enter again;
3. Classification of reentrant locks:
-
Implicit lock: synchronized, based on the JVM level, for lock objects, are hidden, lock and unlock;
-
Display Lock: to Lock implementation class, ReentrantLock, (can be free to Lock and release the Lock, but also better operability;) ReentrantLock
package java.util.concurrent.locks;
public class ReentrantLock implements Lock, java.io.Serializable {
private static final long serialVersionUID = 7373984872572414699L;
/** Synchronizer providing all implementation mechanics */
private final Sync sync;
Copy the code
Proof that Synchronzied is a reentrant lock:
package com.atguowang.thirdinterview.juc;
/ * *
* @author webstart
* @time 2020/10/26/10:41
* @description synchronized validation of reentrant locks
* * /
public class TestSyn {
// Old buddy - thread-operated resource class
static Object dataTest=new Object();
//
static void m1() {
new Thread(()->{
synchronized (dataTest){ System.out.println(Thread.currentThread().getName()+"\t"+"Outer zone"); }
synchronized (dataTest){ System.out.println(Thread.currentThread().getName()+"\t"+"Middle zone"); }
synchronized (dataTest){ System.out.println(Thread.currentThread().getName()+"\t"+"Middle zone"); }
},"lucas").start();
}
public static void main(String[] args) {
m1();
}
}
Copy the code
Display of synchronization method:
package com.atguowang.thirdinterview.juc;
/ * *
* @author webstart
* @time 2020/10/26/10:41
* @description synchronized verification for reentrant locking: synchronization method
* * /
public class TestSyn2 {
public synchronized void m1() {
/ / print
System.out.println("Outside");
m2();
}
public synchronized void m2() {
/ / print
System.out.println("In");
m3();
}
public synchronized void m3() {
/ / print
System.out.println("Within");
}
public static void main(String[] args){
new TestSyn2().m1();
};
}
Copy the code
Use bytecode files to check the running process of synchronized;
ReentrantLock shows how ReentrantLock works:
Special attention:
If the number of times the lock is locked and the number of times the lock is unlocked is not matching, then we can’t get the lock object after the current thread ends, resulting in deadlocks, failed data requests, etc.;