ReentrantLock lock = new ReentrantLock(); // 创建锁lock.lock(); // 加锁System.out.println("ReentrantLock");lock.unlock(); // 释放锁
public void lock() { sync.lock(); // 默认调用非公平锁加锁}
final void lock() { // 如果当前锁没有被持有,则原子性地更新获取锁的状态为1 if (compareAndSetState(0, 1)) setExclusiveOwnerThread(Thread.currentThread()); else // 如果当前锁被持有 todo acquire(1);}
protected final boolean compareAndSetState(int expect, int update) { // 调用unSafe方法更新,将锁被获取的状态更新为1 return unsafe.compareAndSwapInt(this, stateOffset, expect, update);}