本文主要是介绍Java并发基础 - ReentrantLock,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
主要内容:
- ReentrantLock Demo示例
- 公平锁和非公平锁的详细实现
- 公平和非公平的定义
- ReentrantLock使用场景
- 和synchronized的简单比较
一、 ReentrantLock
1. 先看Demo示例,再细细道来原理:
@Slf4j
public class LockDemo {//ReentrantLock无参构造方法,sync = new NonfairSync();默认非公平锁private Lock lock = new ReentrantLock();private void workOn() {log.info(Thread.currentThread().getName() + ":上班!");}private void workOff() {log.info(Thread.currentThread().getName() + ":下班");}private void work() {try {//加锁lock.lock();workOn();log.info(Thread.currentThread().getName() + "工作中...");Thread.sleep(100);workOff();} catch (InterruptedException e) {e.printStackTrace();} finally {//释放锁lock.unlock();}}public static void main(String[] args) {LockDemo lockDemo = new LockDemo();int i = 0;List<Thread> list = new ArrayList<>(30);do {Thread a = new Thread(lockDemo::work, "小A_" + i);Thread b = new Thread(lockDemo::work, "小B_" + i);list.add(a);list.add(b);} while (i++ < 10);list.parallelStream().forEach(Thread::start);}
}
执行之后,可以看到交替输出:
小B_6:上班!
小B_6工作中...
小B_6:下班
小B_7:上班!
小B_7工作中...
小B_7:下班
...省略...
小B_3:上班!
小B_3工作中...
小B_3:下班
即成功的加上了锁。
2. ReentrantLock类的定义
public class ReentrantLock implements Lock, java.io.Serializable {...}
主要是Lock接口,其定义如下:
public interface Lock {void lock();void lockInterruptibly() throws InterruptedException;boolean tryLock();boolean tryLock(long time, TimeUnit unit) throws InterruptedException;void unlock();Condition newCondition();
}
3. ReentrantLock中的三个内部类
//Sync抽象类继承了AQS,使用AQS的state来展示持有锁的数目
abstract static class Sync extends AbstractQueuedSynchronizer{}//非公平锁
static final class NonfairSync extends Sync{}//公平锁
static final class FairSync extends Sync {}
4. NonfairSync类的实现
static final class NonfairSync extends Sync {private static final long serialVersionUID = 7316153563782823691L;//加锁final void lock() {通过CAS来获取同步状态 也就是锁if (compareAndSetState(0, 1))//当前线程占有锁setExclusiveOwnerThread(Thread.currentThread());else//获取失败 进入AQS同步队列排队等待 执行AQS的acquire方法acquire(1);}//获取锁protected final boolean tryAcquire(int acquires) {return nonfairTryAcquire(acquires);}
}
acquire方法:
//AbstractQueuedSynchronizer.java
public final void acquire(int arg) {//tryAcquire方法实际上是NonfairSync类的tryAcquire方法//如果获取不到锁,if (!tryAcquire(arg) &&acquireQueued(addWaiter(Node.EXCLUSIVE), arg))selfInterrupt();
}//ReentrantLock.java
protected final boolean tryAcquire(int acquires) {return nonfairTryAcquire(acquires);
}
非公平锁的获取:
final boolean nonfairTryAcquire(int acquires) {//当前线程final Thread current = Thread.currentThread();int c = getState();//AQS的state=0,代表着锁未被抢占if (c == 0) {//获取到同步状态if (compareAndSetState(0, acquires)) {//当前线程占有锁setExclusiveOwnerThread(current);return true;}}//线程已经占有锁了---重入else if (current == getExclusiveOwnerThread()) {//同步状态记录重入的次数int nextc = c + acquires;if (nextc < 0) // overflowthrow new Error("Maximum lock count exceeded");setState(nextc);return true;}//锁被其他线程抢占了,返回false,未获取到锁return false;
}
非公平锁中,抢到AQS的同步状态的未必是同步队列的首节点,只要线程通过CAS抢到了同步状态或者在acquire中抢到同步状态,就优先占有锁,而相对同步队列这个严格的FIFO队列来说,所以会被认为是非公平锁。
5. FairSync类的实现
static final class FairSync extends Sync {private static final long serialVersionUID = -3000897897090466540L;final void lock() {//严格按照AQS的同步队列要求去获取同步状态。加锁的时候就调用了下面的tryAcquire方法acquire(1);}protected final boolean tryAcquire(int acquires) {final Thread current = Thread.currentThread();int c = getState();//锁未被抢占if (c == 0) {//没有前驱节点,并且CAS获取到了同步状态,则独占if (!hasQueuedPredecessors() && compareAndSetState(0, acquires)) {setExclusiveOwnerThread(current);return true;}}//锁已被抢占且线程重入else if (current == getExclusiveOwnerThread()) {int nextc = c + acquires;if (nextc < 0)throw new Error("Maximum lock count exceeded");setState(nextc);return true;}return false;}
}
公平锁的实现直接调用AQS的acquire方法,acquire中调用tryAcquire。和非公平锁相比,这里不会执行一次CAS,接下来在tryAcquire去抢占锁的时候,也会先调用hasQueuedPredecessors看看前面是否有节点已经在等待获取锁了,如果存在则同步队列的前驱节点优先。
//队列中是否有前置线程。当前线程队列头,或者队列为空,返回false
public final boolean hasQueuedPredecessors() {Node t = tail;Node h = head;Node s;return h != t &&((s = h.next) == null || s.thread != Thread.currentThread());
}
二、 应用方式
1. 普通方式
//默认Nonfair,传入true使用Fair
Lock lock = new ReentrantLock();
try {lock.lock();//……}finally {lock.unlock();}
2. 带返回结果的锁
- tryLock:尝试获取非公平锁,直接返回获取结果
public boolean tryLock() {return sync.nonfairTryAcquire(1);
}
- tryLock(long timeout, TimeUnit unit):带超时时间的
public boolean tryLock(long timeout, TimeUnit unit)throws InterruptedException {return sync.tryAcquireNanos(1, unit.toNanos(timeout));
}
- acquireInterruptibly(int arg):如果该线程未被中断则获取锁
三、和synchronized的比较
ReentrantLock | synchronized |
---|---|
显式的使用在同步方法或者同步代码块中 | 显式的指定起始和结束位置 |
托管给JVM执行,不会因为异常、或者未释放而发生死锁 | 手动释放锁 |
都是互斥同步(悲观锁)也叫做阻塞同步锁,特征是会对没有获取锁的线程进行阻塞。
性能不是选择他们的原因,如果不是synchronized无法实现的功能,如轮询锁、超时锁和中断锁等,推荐首先使用synchronized,而针对锁的高级功能,再使用ReentrantLock。
这篇关于Java并发基础 - ReentrantLock的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!