并发编程篇:重入锁ReentrantLock

x33g5p2x  于2021-12-18 转载在 其他  
字(1.6k)|赞(0)|评价(0)|浏览(419)

定义

1、重入锁ReentrantLock,支持重入的锁,标识该锁能够支持一个线程对资源重复加锁,该锁支持获取锁的公平性和非公平性选择。synchronized关键字隐式的支持重进入。
2、公平锁,在绝对的时间上,先对锁进行请求的一定先满足;反之,就是不公平的。公平锁机制往往没有非公平锁的效率高。公平锁往往没有非公平锁的效率高,公平锁能够减少”饥饿“发生的概率,等待越久的请求越是能够得到优先满足。

实现重入锁

重入锁是指任意线程在获取到锁之后能够再次获取该锁而不会被锁阻塞。该特性需要解决2个问题

  1. 线程再次获取锁:锁需要去识别获取锁的线程是否为当前占据锁的线程,如果是,则再次获取成功。
  2. 锁的最终释放:线程重复n次获取了锁,随后在第n次释放该锁后,其他线程能够再次获取到该锁。
锁的获取(以默认的非公平为例子)
  1. final boolean nonfairTryAcquire(int acquires) {
  2. final Thread current = Thread.currentThread();
  3. // 获取锁的状态,
  4. int c = getState();
  5. // 如果锁还在
  6. if (c == 0) {
  7. // 设置锁的状态
  8. if (compareAndSetState(0, acquires)) {
  9. // 设置当前线程为锁的独占线程
  10. setExclusiveOwnerThread(current);
  11. return true;
  12. }
  13. }
  14. // 如果锁的独占线程是当前线程
  15. else if (current == getExclusiveOwnerThread()) {
  16. int nextc = c + acquires;
  17. if (nextc < 0) // overflow
  18. throw new Error("Maximum lock count exceeded");
  19. //设置锁的状态
  20. setState(nextc);
  21. return true;
  22. }
  23. return false;
  24. }
锁的释放

如果该锁获取了n次,那么当前(n-1)次tryRelease方法返回false,而之后全部释放了,才会返回true。

  1. protected final boolean tryRelease(int releases) {
  2. int c = getState() - releases;
  3. if (Thread.currentThread() != getExclusiveOwnerThread())
  4. throw new IllegalMonitorStateException();
  5. boolean free = false;
  6. if (c == 0) {
  7. free = true;
  8. setExclusiveOwnerThread(null);
  9. }
  10. setState(c);
  11. return free;
  12. }

公平锁和非公平锁的区别

公平性与否是针对锁的获取而言的,如果一个锁是公平的,那么锁的获取顺序应该符合请求的绝对次序,也就是FIFO。

  1. protected final boolean tryAcquire(int acquires) {
  2. final Thread current = Thread.currentThread();
  3. int c = getState();
  4. if (c == 0) {
  5. // 查看是否有后继节点,如果没有才获取锁
  6. if (!hasQueuedPredecessors() &&
  7. compareAndSetState(0, acquires)) {
  8. setExclusiveOwnerThread(current);
  9. return true;
  10. }
  11. }
  12. else if (current == getExclusiveOwnerThread()) {
  13. int nextc = c + acquires;
  14. if (nextc < 0)
  15. throw new Error("Maximum lock count exceeded");
  16. setState(nextc);
  17. return true;
  18. }
  19. return false;
  20. }
  21. }

欢迎关注公众号“程序员ZZ的源码”,一起来学习

相关文章