Lock接口
- 与synchronize相比优势之处
- 可以尝试非阻塞的获取锁
- 在获取锁的过程中能够中断
- 超时获取锁
队列同步器
- 内部结构:
- int成员变量表示同步状态
- FIFO对列来完成资源获取线程的排队工作
队列同步器的接口与示范
- 同步器的设计是基于模板方法模式的。在使用时,需要继承同步器并重写指定的方法,就可以实现不同的功能。
- 同步状态操作的三个方法
- getState()
- setState(int newState)
- compareAndSetState(int expect,int update)
- 同步器可重写的方法,开发过程中,根据自身的逻辑设计自己的锁,这些方法都是方便我们去操作state变量,已经在获取成功锁之后,更新state变量的值。对于同步对列的加入和同步队列中节点的等待都由AQS实现。
- tryAcquire(int arg):独占式获取同步状态,成功返回true,失败返回false,不进行其他操作
- tryRelease(int arg):独占式释放同步状态,同样的释放成功返回true,失败返回false。
- tryAcquireShared(int arg),共享式获取同步状态,返回大于等于0 的值,表示获取成功,否则,获取失败。
- tryReleaseShared(int arg):共享式释放同步状态
- isHeldExclusively():当前同步器是否在独占模式下呗线程占有,一般该方法表示是否被当前线程锁占有。
- 同步器提供的模板方法,即使用AQS实现自己的锁时,共同使用的方法,我们使用模板方法,在模板方法中调用我们自己实现的重写的方法来实现逻辑。
- acquire(int arg):独占式获取同步状态,获取成功,该方法返回,获取失败,将会进入同步队列中。该方法会调用重写的tryAcquire(int arg)方法
public final void acquire(int arg) { //自己的锁,重写的方法 if (!tryAcquire(arg) && acquireQueued(addWaiter(Node.EXCLUSIVE), arg)) selfInterrupt(); }
- acquireInterruptibly(int arg): 与上面类似,该方法可以响应中断,成功则返回,失败则加入到同步队列,如果当前线程被中断,则抛出InterruptedException异常。
public final void acquireInterruptibly(int arg) throws InterruptedException { //同样调用重写的方法 if (Thread.interrupted()) throw new InterruptedException(); if (!tryAcquire(arg)) doAcquireInterruptibly(arg); }
- tryAcquireNanos(int arg,long nanos):在acquireInterruptibly的基础上增加了超时限制
public final boolean tryAcquireNanos(int arg, long nanosTimeout) throws InterruptedException { if (Thread.interrupted()) throw new InterruptedException(); //获取成功 return tryAcquire(arg) || //超时获取锁,获取失败也不加入同步队列 doAcquireNanos(arg, nanosTimeout); }
- acquireShared(int arg):共享式获取同步状态,如果当前线程未获取则进入同步状态,与独占锁不同的是在同一时刻可以有多个线程获取到同步状态。
public final void acquireShared(int arg) { //调用我们重写的方法,如果小于0则表示获取失败 if (tryAcquireShared(arg) < 0) //获取失败加入同步队列 doAcquireShared(arg); }
- acquireSharedInterruptibly(int arg):方法响应中断,同样也调用重写方法
public final void acquireSharedInterruptibly(int arg) throws InterruptedException { //出现中断报异常 if (Thread.interrupted()) throw new InterruptedException(); //大于等于0表示获取成功 if (tryAcquireShared(arg) < 0) //获取失败,加入同步对列中进行获取 doAcquireSharedInterruptibly(arg); }
- tryAcquireSharedNanos(int arg,long nanos):acquireSharedInterruptibly的基础上增加了超时实现
public final boolean tryAcquireSharedNanos(int arg, long nanosTimeout) throws InterruptedException { if (Thread.interrupted()) throw new InterruptedException(); //调用重写的共享式获取的方法 return tryAcquireShared(arg) >= 0 || doAcquireSharedNanos(arg, nanosTimeout); }
- release(int arg):独占式释放同步状态,该方法会在释放同步状态后,将同步队列中的第一个节点包含的线程唤醒。
public final boolean release(int arg) { //调用重写的方法 if (tryRelease(arg)) { Node h = head; //如果当前节点不为空,唤醒他的后继结点 if (h != null && h.waitStatus != 0) unparkSuccessor(h); return true; } return false; }
- acquire(int arg):独占式获取同步状态,获取成功,该方法返回,获取失败,将会进入同步队列中。该方法会调用重写的tryAcquire(int arg)方法