JUC - ReentrantLock源码解析 - 多图警告

x33g5p2x  于2022-02-12 转载在 其他  
字(2.8k)|赞(0)|评价(0)|浏览(406)

ReentrantLock分类FairSync公平锁 - 构造函数true - 线程轮流获得锁NonfairSync非公平锁 - 默认,构造函数false - 跟Synchronized一样是非公平

2.1.1.0 构造函数

2.1.1.1 lockInterruptibly - 可中断锁 - 正在等待获取锁的线程可直接调用Thread.interrupt该线程直接放弃获取锁,且直接抛出异常

ReentrantLock

AbstractQueuedSynchronizer

tryAcquire

NonfairSync

Sync

doAcquireInterruptibly

2.1.1.2 lock - 等待锁的线程在另一个线程被interrupt不会立刻终止的原因,只有获取到锁然后才会终止

2.1.1.3 unlock

Sync

2.1.1.5 如果你看懂了前面的代码解析,这段代码的输出你应该也能看懂

区别1. lockInterruptibly:等待获取锁的线程1,在此过程中,如果被其他线程nterrupt,则该线程1立刻直接不等待获取锁,丢弃锁,直接抛出异常2. lock:等待获取锁的线程1,在此过程中,如果被其他线程interrupt,则延迟到获取锁才interrupt

  1. package top.linruchang.springdemo;
  2. import lombok.extern.slf4j.Slf4j;
  3. import java.util.concurrent.locks.ReentrantLock;
  4. /**
  5. * 作用:
  6. *
  7. * @author LinRuChang
  8. * @version 1.0
  9. * @date 2021/03/14
  10. * @since 1.8
  11. **/
  12. @Slf4j
  13. public class OtherTest3 {
  14. public static void main(String[] args) throws InterruptedException {
  15. System.out.println("\n=========LockInterruptibly===========\n");
  16. testLockInterruptibly();
  17. Thread.sleep(15000);
  18. System.out.println("\n==========Lock==========\n");
  19. testLock();
  20. }
  21. public static void testLockInterruptibly() throws InterruptedException {
  22. TestLock testLock = new TestLock();
  23. Thread thread1 = new Thread(() -> {
  24. testLock.testLockInterruptibly();
  25. });
  26. thread1.start();
  27. Thread.sleep(500);
  28. Thread thread2 = new Thread(() -> {
  29. testLock.testLockInterruptibly();
  30. });
  31. thread2.start();
  32. Thread.sleep(500);
  33. log.info("中断第二个线程");
  34. thread2.interrupt();
  35. }
  36. public static void testLock() throws InterruptedException {
  37. TestLock testLock = new TestLock();
  38. Thread thread1 = new Thread(() -> {
  39. testLock.testLock();
  40. });
  41. thread1.start();
  42. Thread.sleep(500);
  43. Thread thread2 = new Thread(() -> {
  44. testLock.testLock();
  45. });
  46. thread2.start();
  47. Thread.sleep(500);
  48. log.info("中断第二个线程");
  49. thread2.interrupt();
  50. }
  51. }
  52. @Slf4j
  53. class TestLock {
  54. ReentrantLock reentrantLock = new ReentrantLock();
  55. public void testLockInterruptibly() {
  56. String threadName = Thread.currentThread().getName();
  57. log.info(threadName + "启动");
  58. try {
  59. //获取到锁,但可以调用interceprt使得锁失效
  60. reentrantLock.lockInterruptibly();
  61. log.info(threadName + ":休眠10s");
  62. Thread.sleep(10000);
  63. log.info(threadName + ":休眠起来啦");
  64. } catch (Exception e) {
  65. log.error(threadName + ":发生异常" + e);
  66. } finally {
  67. reentrantLock.unlock();
  68. log.info(threadName + "结束,并释放锁");
  69. }
  70. }
  71. public void testLock() {
  72. String threadName = Thread.currentThread().getName();
  73. log.info(threadName + "启动");
  74. try {
  75. //获取到锁,但可以调用interceprt使得锁失效
  76. reentrantLock.lock();
  77. log.info(threadName + ":第一个休眠10s");
  78. try {
  79. Thread.sleep(10000);
  80. }catch (Exception e) {
  81. log.error(threadName + ":发生异常【第一个休眠】" + e);
  82. }
  83. log.info(threadName + ":第一个休眠起来啦");
  84. log.info(threadName + ":第二个休眠10s");
  85. try {
  86. Thread.sleep(10000);
  87. }catch (Exception e) {
  88. log.error(threadName + ":发生异常【第二个休眠】" + e);
  89. }
  90. log.info(threadName + ":第二个休眠起来啦");
  91. } catch (Exception e) {
  92. log.error(threadName + ":发生异常" + e);
  93. } finally {
  94. reentrantLock.unlock();
  95. log.info(threadName + "结束,并释放锁");
  96. }
  97. }
  98. }

相关文章