ReentrantLock

一直在用concurrent包里的东西,最近想研究一下个中细节,先从ReentrantLock提供的集中获取锁的方式开始吧。

1.ReentrantLock简要介绍

简单介绍一下ReentrantLock,可重入锁,互斥锁,提供了fair和unfair两种模式的锁。默认构造函数是unfair的锁,如果初始化时传入true的参数则会返回fair锁。所谓不公平就是在锁可获取时,不用考虑该锁队列是否有其他waiter,直接获取;反之,对于公平锁来讲就是当等待的锁资源可获取时要看下等待队列中当前线程是不是head线程,如果不是则不获取。

简单的看一下获取锁的代码:


   
   
  1. //fair lock tryAcquire
  2. if (c == 0) {
  3. if (!hasQueuedPredecessors() &&
  4. compareAndSetState( 0, acquires)) {
  5. setExclusiveOwnerThread(current);
  6. return true;
  7. }
  8. }

   
   
  1. //unfair lock nonfairTryAcquire
  2. if (c == 0) {
  3. if (compareAndSetState( 0, acquires)) {
  4. setExclusiveOwnerThread(current);
  5. return true;
  6. }
  7. }

从上面的代码块可以清晰的看到,公平锁fairLock在获取之前会看下自己是不是没有前继元素。而非公平锁unfairLock并不会。

2.ReentrantLock提供的获取锁的方式

ReentrantLock提供了lock()、tryLock()、tryLock(long timeout, TimeUnit unit)、lock.lockInterruptibly()

1)lock()


   
   
  1. public void lock() {
  2. sync.lock();
  3. }
  • 当锁可用,并且当前线程没有持有该锁,直接获取锁并把count set为1.
  • 当锁可用,并且当前线程已经持有该锁,直接获取锁并把count增加1.
  • 当锁不可用,那么当前线程被阻塞,休眠一直到该锁可以获取,然后把持有count设置为1.

小结:该种方式获取锁不可中断,如果获取不到则一直休眠等待。

2)tryLock() 


   
   
  1. public boolean tryLock() {
  2. return sync.nonfairTryAcquire( 1);
  3. }

   
   
  1. /**
  2. * Performs non-fair tryLock. tryAcquire is implemented in
  3. * subclasses, but both need nonfair try for trylock method.
  4. */
  5. final boolean nonfairTryAcquire(int acquires) {
  6. final Thread current = Thread.currentThread();
  7. int c = getState();
  8. if (c == 0) {
  9. if (compareAndSetState( 0, acquires)) {
  10. setExclusiveOwnerThread(current);
  11. return true;
  12. }
  13. }
  14. else if (current == getExclusiveOwnerThread()) {
  15. int nextc = c + acquires;
  16. if (nextc < 0) // overflow
  17. throw new Error( "Maximum lock count exceeded");
  18. setState(nextc);
  19. return true;
  20. }
  21. return false;
  22. }
  • 当获取锁时,只有当该锁资源没有被其他线程持有才可以获取到,并且返回true,同时设置持有count为1;
  • 当获取锁时,当前线程已持有该锁,那么锁可用时,返回true,同时设置持有count加1;
  • 当获取锁时,如果其他线程持有该锁,无可用锁资源,直接返回false,这时候线程不用阻塞等待,可以先去做其他事情;
  • 即使该锁是公平锁fairLock,使用tryLock()的方式获取锁也会是非公平的方式,只要获取锁时该锁可用那么就会直接获取并返回true。这种直接插入的特性在一些特定场景是很有用的。但是如果就是想使用公平的方式的话,可以试一试tryLock(0, TimeUnit.SECONDS),几乎跟公平锁没区别,只是会监测中断事件。

3)tryLock(long timeout, TimeUnit unit)


   
   
  1. public boolean tryLock(long timeout, TimeUnit unit)
  2. throws InterruptedException {
  3. return sync.tryAcquireNanos( 1, unit.toNanos(timeout));
  4. }

   
   
  1. public final boolean tryAcquireNanos(int arg, long nanosTimeout)
  2. throws InterruptedException {
  3. if (Thread.interrupted())
  4. throw new InterruptedException();
  5. return tryAcquire(arg) ||
  6. doAcquireNanos(arg, nanosTimeout);
  7. }

   
   
  1. private boolean doAcquireNanos(int arg, long nanosTimeout)
  2. throws InterruptedException {
  3. if (nanosTimeout <= 0L)
  4. return false;
  5. final long deadline = System.nanoTime() + nanosTimeout;
  6. final Node node = addWaiter(Node.EXCLUSIVE);
  7. boolean failed = true;
  8. try {
  9. for (;;) {
  10. final Node p = node.predecessor();
  11. if (p == head && tryAcquire(arg)) {
  12. setHead(node);
  13. p.next = null; // help GC
  14. failed = false;
  15. return true;
  16. }
  17. nanosTimeout = deadline - System.nanoTime();
  18. if (nanosTimeout <= 0L)
  19. return false;
  20. if (shouldParkAfterFailedAcquire(p, node) &&
  21. nanosTimeout > spinForTimeoutThreshold)
  22. LockSupport.parkNanos( this, nanosTimeout);
  23. if (Thread.interrupted())
  24. throw new InterruptedException();
  25. }
  26. } finally {
  27. if (failed)
  28. cancelAcquire(node);
  29. }
  30. }
  • 从上面代码中可以看出,获取锁成功或者超时之后返回。而且在公平锁和非公平锁的场景下都可以使用,只是会增加对中断事件的监测。
  • 当获取锁时,锁资源在超时时间之内变为可用,并且在等待时没有被中断,那么当前线程成功获取锁,返回true,同时当前线程持有锁的count设置为1.
  • 当获取锁时,在超时时间之内没有锁资源可用,那么当前线程获取失败,不再继续等待,返回false.
  • 当获取锁时,在超时等待时间之内,被中断了,那么抛出InterruptedException,不再继续等待.
  • 当获取锁时,在超时时间之内锁可用,并且当前线程之前已持有该锁,那么成功获取锁,同时持有count加1.

4)lockInterruptibly()


   
   
  1. public void lockInterruptibly() throws InterruptedException {
  2. sync.acquireInterruptibly( 1);
  3. }

   
   
  1. public final void acquireInterruptibly(int arg)
  2. throws InterruptedException {
  3. if (Thread.interrupted())
  4. throw new InterruptedException();
  5. if (!tryAcquire(arg))
  6. doAcquireInterruptibly(arg);
  7. }

   
   
  1. private void doAcquireInterruptibly(int arg)
  2. throws InterruptedException {
  3. final Node node = addWaiter(Node.EXCLUSIVE);
  4. boolean failed = true;
  5. try {
  6. for (;;) {
  7. final Node p = node.predecessor();
  8. if (p == head && tryAcquire(arg)) {
  9. setHead(node);
  10. p.next = null; // help GC
  11. failed = false;
  12. return;
  13. }
  14. if (shouldParkAfterFailedAcquire(p, node) &&
  15. parkAndCheckInterrupt())
  16. throw new InterruptedException();
  17. }
  18. } finally {
  19. if (failed)
  20. cancelAcquire(node);
  21. }
  22. }
  • 当获取锁时,锁资源可用,那么当前线程成功获得锁,同时持有count设置为1,返回true.
  • 当获取锁时,锁资源可用,当前线程已持有该锁,它成功获取该锁,同时持有count增加1,返回true.
  • 当获取锁时,锁资源不可用,那么该线程开始阻塞休眠等待,但是等待过程中如果有中断事件,那么会停止等待,立即返回.
  • 当获取锁时,锁资源不可用,线程开始阻塞休眠等待,如果等待过程中锁资源变为可用,那么当前线程成功获得锁,同时持有count设置为1,返回true.

小结:lockInterruptibly()获取锁是以排他的模式获取,一旦被中断就放弃等待获取。在等待开始时首先检测中断状态,然后至少调用一次tryAcquire,成功获取就返回true。否则当前线程就开始排队,并且不断的被blocking、unblocking、invoking tryAcquire 直到获取成功或者被中断为止。

3.使用场景示例

下面提供三个demo分别使用lock(), tryLock()的方式获取锁资源,示例展示了持续等待,立即返回,限时等待,中断等场景的使用。

1.如果当前获得锁的线程在做大量耗时的工作,使用lock.lock()方法申请锁的线程会一直阻塞,这样就降低了多线程的效率。而使用tryLock()方法申请锁,如果锁不可用则线程不会阻塞,转而可以去做其他工作。代码实例如下:


   
   
  1. public class TestLockAndTryLock {
  2. private ReentrantLock rlock = new ReentrantLock();
  3. private void lockTest(){
  4. long currentTime = System.currentTimeMillis();
  5. try {
  6. rlock.lock();
  7. while (System.currentTimeMillis() - currentTime <= 1000){
  8. //assume do something
  9. }
  10. System.out.println( "lockTest----current thread get the lock: " + Thread.currentThread().getName());
  11. } finally {
  12. rlock.unlock();
  13. System.out.println( "lockTest----current thread release the lock: " + Thread.currentThread().getName());
  14. }
  15. }
  16. private void tryLockTest(){
  17. long currentTime = System.currentTimeMillis();
  18. while (System.currentTimeMillis() - currentTime <= 100){
  19. //assume do something
  20. }
  21. if (rlock.tryLock()){
  22. try {
  23. System.out.println( "tryLockTest----current thread get the lock: " + Thread.currentThread().getName());
  24. } finally {
  25. rlock.unlock();
  26. System.out.println( "tryLockTest----current thread release the lock: " + Thread.currentThread().getName());
  27. }
  28. } else {
  29. System.out.println( "tryLockTest----current thread CAN NOT get the lock: " + Thread.currentThread().getName());
  30. }
  31. }
  32. public static void main(String[] args){
  33. TestLockAndTryLock lockAndTryLock = new TestLockAndTryLock();
  34. Thread lockThread = new Thread(
  35. () -> lockAndTryLock.lockTest(), "Lock-Thread" );
  36. Thread tryLockThread = new Thread(
  37. () -> lockAndTryLock.tryLockTest(), "TryLock-Thread" );
  38. tryLockThread.start();
  39. lockThread.start();
  40. }
  41. }

   
   
  1. output:
  2. tryLockTest----current thread CAN NOT get the lock: TryLock-Thread
  3. lockTest----current thread get the lock: Lock-Thread
  4. lockTest----current thread release the lock:  Lock-Thread

lock方法不能被中断。如果一个线程在等待获得一个锁时被中断,中断线程在获得锁之前会一直处于 阻塞状态。如果出现死锁,那么lock方法就无法被终止。但是tryLock(long,TimeUnit)在等待超时之后可以结束等待。demo如下:


   
   
  1. public class TestLockAndTryLock {
  2. private ReentrantLock rlock = new ReentrantLock();
  3. private void lockTest(){
  4. long currentTime = System.currentTimeMillis();
  5. try {
  6. rlock.lock();
  7. System.out.println( "lockTest----current thread get the lock: " + Thread.currentThread().getName());
  8. while (System.currentTimeMillis() - currentTime <= 5000){
  9. //assume do something
  10. }
  11. } finally {
  12. rlock.unlock();
  13. System.out.println( "lockTest----current thread release the lock: " + Thread.currentThread().getName());
  14. }
  15. }
  16. private void tryLockTest(){
  17. long currentTime = System.currentTimeMillis();
  18. while (System.currentTimeMillis() - currentTime <= 100){
  19. //assume do something
  20. }
  21. if (rlock.tryLock()){
  22. try {
  23. System.out.println( "tryLockTest----current thread get the lock: " + Thread.currentThread().getName());
  24. } finally {
  25. rlock.unlock();
  26. System.out.println( "tryLockTest----current thread release the lock: " + Thread.currentThread().getName());
  27. }
  28. } else {
  29. System.out.println( "tryLockTest----current thread CAN NOT get the lock: " + Thread.currentThread().getName());
  30. }
  31. }
  32. private void tryLockInterruptTest(){
  33. long currentTime = System.currentTimeMillis();
  34. while (System.currentTimeMillis() - currentTime <= 100){
  35. //assume do something
  36. }
  37. try {
  38. System.out.println( "Begin time: " + System.currentTimeMillis());
  39. if (rlock.tryLock( 1, TimeUnit.SECONDS)){
  40. try {
  41. System.out.println( "tryLockInterruptTest----current thread get the lock: " + Thread.currentThread().getName());
  42. } finally {
  43. rlock.unlock();
  44. System.out.println( "tryLockInterruptTest----current thread release the lock: " + Thread.currentThread().getName());
  45. }
  46. } else {
  47. System.out.println( "End time: " + System.currentTimeMillis());
  48. System.out.println( "tryLockInterruptTest----current thread CAN NOT get the lock: " + Thread.currentThread().getName());
  49. }
  50. } catch (InterruptedException e) {
  51. e.printStackTrace();
  52. }
  53. }
  54. public static void main(String[] args){
  55. TestLockAndTryLock lockAndTryLock = new TestLockAndTryLock();
  56. Thread lockThread = new Thread(
  57. () -> lockAndTryLock.lockTest(), "Lock-Thread" );
  58. Thread tryLockInterruptThread = new Thread(
  59. () -> lockAndTryLock.tryLockInterruptTest(), "TryLockInterrupt-Thread"
  60. );
  61. tryLockInterruptThread.start();
  62. lockThread.start();
  63. }
  64. }

   
   
  1. output:
  2. lockTest ----current thread get the lock: Lock-Thread
  3. Begin time: 1533636472680
  4. End time: 1533636473681
  5. tryLockInterruptTest ----current thread CAN NOT get the lock: TryLockInterrupt-Thread
  6. lockTest ----current thread release the lock: Lock-Thread

同时,tryLock(long, TimeUnit)可以被中断,demo如下:


   
   
  1. private void tryLockInterruptTest(){
  2. long currentTime = System.currentTimeMillis();
  3. while (System.currentTimeMillis() - currentTime <= 100){
  4. //assume do something
  5. }
  6. try {
  7. System.out.println( "Begin time: " + System.currentTimeMillis());
  8. if (rlock.tryLock( 3, TimeUnit.SECONDS)){
  9. try {
  10. System.out.println( "tryLockInterruptTest----current thread get the lock: " + Thread.currentThread().getName());
  11. } finally {
  12. rlock.unlock();
  13. System.out.println( "tryLockInterruptTest----current thread release the lock: " + Thread.currentThread().getName());
  14. }
  15. } else {
  16. System.out.println( "End time: " + System.currentTimeMillis());
  17. System.out.println( "tryLockInterruptTest----current thread CAN NOT get the lock: " + Thread.currentThread().getName());
  18. }
  19. } catch (InterruptedException e) {
  20. System.out.println( "tryLockInterruptTest Interrupt----current thread is interrupted: " + Thread.currentThread().getName());
  21. }
  22. }
  23. public static void main(String[] args){
  24. TestLockAndTryLock lockAndTryLock = new TestLockAndTryLock();
  25. Thread lockThread = new Thread(
  26. () -> lockAndTryLock.lockTest(), "Lock-Thread" );
  27. Thread tryLockInterruptThread = new Thread(
  28. () -> lockAndTryLock.tryLockInterruptTest(), "TryLockInterrupt-Thread"
  29. );
  30. tryLockInterruptThread.start();
  31. lockThread.start();
  32. try {
  33. Thread.sleep( 100);
  34. } catch (InterruptedException e) {
  35. System.out.println(Thread.currentThread().getName() + "is interrupted now. ");
  36. }
  37. tryLockInterruptThread.interrupt();
  38. }

   
   
  1. output:
  2. lockTest ----current thread get the lock: Lock-Thread
  3. Begin time: 1533637530378
  4. tryLockInterruptTest Interrupt ----current thread is interrupted: TryLockInterrupt-Thread
  5. lockTest ----current thread release the lock: Lock-Thread

很明显被中断了,没有完成等待。

以上是对ReentrantLock的几种获取锁的方法的详解,并附以demo示例,如有疑问可以留言讨论。

 

refer:https://blog.csdn.net/u011784767/article/details/51659701

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值