代码层面分析可重入原理
final boolean nonfairTryAcquire(int acquires) { //获取当前的线程 final Thread current = Thread.currentThread(); //获取状态 0 代表当前线程可以获得这把锁 int c = getState(); if (c == 0) { if (compareAndSetState(0, acquires)) {//CAS //设置独占 setExclusiveOwnerThread(current); return true; } } //如果当前状态不为0,则判断当前线程是否是正独占这把锁的线程 else if (current == getExclusiveOwnerThread()) { int nextc = c + acquires; if (nextc < 0) // overflow //线程状态小于-1 throw new Error("Maximum lock count exceeded"); //设置状态 setState(nextc); return true; } return false;}