Java并发编程 AQS分析(二):获取锁、释放锁

来源:互联网 发布:软件构架实践 epub 编辑:程序博客网 时间:2024/06/05 09:34

上篇博客稍微介绍了一下AQS,下面我们来关注下AQS的所获取和锁释放。

AQS锁获取

AQS包含如下几个方法:

acquire(int arg):以独占模式获取对象,忽略中断。

acquireInterruptibly(int arg): 以独占模式获取对象,如果被中断则中止。

acquireShared(int arg): 以共享模式获取对象,忽略中断。

acquireSharedInterruptibly(int arg)以共享模式获取对象,如果被中断则中止。

tryAcquire(int arg):试图在独占模式下获取对象状态。

tryAcquireNanos(int arg, long nanosTimeout):试图以独占模式获取对象,如果被中断则中止,如果到了给定超时时间,则会失败。

tryAcquireShared(int arg):试图在共享模式下获取对象状态。

tryAcquireSharedNanos(int arg, long nanosTimeout):试图以共享模式获取对象,如果被中断则中止,如果到了给定超时时间,则会失败。

对于lock.lock()最终都会调用AQS的acquire()方法,Semaphore.acquire()最终会调用AQS的acquireSharedInterruptibly()方法,其中acquire()源代码如下:

[java] view plain copy
  1. public final void acquire(int arg) {  
  2.         if (!tryAcquire(arg) &&  
  3.             acquireQueued(addWaiter(Node.EXCLUSIVE), arg))  
  4.             selfInterrupt();  
  5.     }  

tryAcquire:去尝试获取锁,获取成功则设置锁状态并返回true,否则返回false。

addWaiter:将当前线程加入到CLH队列队尾。

acquireQueued:当前线程会根据公平性原则来进行阻塞等待,直到获取锁为止;并且返回当前线程在等待过程中有没有中断过。

selfInterrupt:产生一个中断。

其主要流程如下:

2015120400001

1、首先线程尝试获取锁,如果成功则直接返回,不成功则新建一个Node节点并添加到CLH队列中。tryAcquire尝试获取锁,addWaiter则新建节点并添加到CLH队列中。其中tryAcquire,AQS并没有提供实现,它仅仅只是抛出一个异常,具体的实现需要各个锁自己实现。

[java] view plain copy
  1. protected boolean tryAcquire(int arg) {  
  2.         throw new UnsupportedOperationException();  
  3.     }  

addWaiter后面讲述。

2、acquireQueued主要功能是根据该节点寻找CLH队列的头结点,并且尝试获取锁,判断是否需要挂起,并且返回挂起标识。如下:

[java] view plain copy
  1. final boolean acquireQueued(final Node node, int arg) {  
  2.         try {  
  3.             boolean interrupted = false;  
  4.             for (;;) {  
  5.                 final Node p = node.predecessor();  
  6.                 if (p == head && tryAcquire(arg)) {  
  7.                     setHead(node);  
  8.                     p.next = null// help GCreturn interrupted;  
  9.                 }  
  10.                 if (shouldParkAfterFailedAcquire(p, node) &&  
  11.                     parkAndCheckInterrupt())  
  12.                     interrupted = true;  
  13.             }  
  14.         } catch (RuntimeException ex) {  
  15.             cancelAcquire(node);  
  16.             throw ex;  
  17.         }  
  18.     }  

在acquireQueued()内部仍然调用tryAcquire()来获取锁。更多详情请参考:【Java并发编程实战】—–“J.U.C”:ReentrantLock之二lock方法分析

selfInterrupt:产生一个中断。如果在acquireQueued()中当前线程被中断过,则需要产生一个中断。

[java] view plain copy
  1. private static void selfInterrupt() {  
  2.     Thread.currentThread().interrupt();  
  3. }  

AQS锁释放

AQS释放锁的方法主要有:

release(int arg):以独占模式释放对象。

releaseShared(int arg): 以共享模式释放对象

tryRelease(int arg):试图设置状态来反映独占模式下的一个释放。

tryReleaseShared(int arg):试图设置状态来反映共享模式下的一个释放。

释放锁相对于获取锁来说还是比较简单的,其主要流程如下:

2015120400002

其代码如下(release()):

[java] view plain copy
  1. public final boolean release(int arg) {  
  2.         if (tryRelease(arg)) {  
  3.             Node h = head;  
  4.             if (h != null && h.waitStatus != 0)  
  5.                 unparkSuccessor(h);  
  6.             return true;  
  7.         }  
  8.         return false;  
  9.     }  

tryeRelease():尝试释放锁,AQS也同样没有提供实现,具体实现方法要其子类自己内部实现,AQS仅仅只是抛出一个异常。

[java] view plain copy
  1. protected boolean tryRelease(int arg) {  
  2.         throw new UnsupportedOperationException();  
  3.     }  


阅读全文
0 0
原创粉丝点击