AQS的原理和使用

什么是AQS

全称AbstractQueuedSynchronizer在这里插入图片描述

AQS实现是基于CLH队列锁
在这里插入图片描述

CLH同步队列是一个FIFO双向队列,这三张图是来解释状态改变的过程,关于CLH可以自行查询
在这里插入图片描述

多个线程时,myPred指向上一个存储的线程,并且自循检查前一个线程是否释放锁,自循一定次数后进入阻塞状态
在这里插入图片描述

上一个线程的locked等于false时,当前线程就可以获取到锁。

AQS使用方式

实现见下面锁可重入示例代码

AQS的设计模式

模版模式:具体实现交给子类去实现。自己要实现同步锁,重写模版方法就可以实现。

AQS中的核心方法

tryAcquire和tryRelease方法。
AQS主要通过核心成员变量state来判断是否获取到锁。
在这里插入图片描述

了解ReentrantLock锁
  • 锁的公平与非公平
  1. 公平锁
    在这里插入图片描述
  2. 非公平锁
    在这里插入图片描述

区别在于公平锁判断当前线程在队列中前面是否还有其他线程

  • 锁的可重入

同一个加锁方法,递归调用依然可以拿到锁,不会出现卡死的情况。

  1. 例子:
static final Lock lock = new ReenterSelfLock();

public void reenter(int x) {
      lock.lock();
      try {
            System.out.println(Thread.currentThread().getName()+":递归层级:"+x);
            int y = x - 1;
            if (y==0) return;
            else{
                reenter(y);
            }
        } finally {
            lock.unlock();
        }
}

public void test() {
        class Worker extends Thread {
			public void run() {
                System.out.println(Thread.currentThread().getName());
                SleepTools.second(1);
                reenter(3);
            }
        }
        // 启动3个子线程
        for (int i = 0; i < 3; i++) {
            Worker w = new Worker();
            w.start();
        }
  1. 如何实现可重入锁:
/**
 *类说明:实现独占锁,可重入
 */
public class ReenterSelfLock implements Lock {
    /* 静态内部类,自定义同步器*/
    private static class Sync extends AbstractQueuedSynchronizer {

        /* 是否处于占用状态*/
        protected boolean isHeldExclusively() {
            return getState() > 0;
        }

        /* 当状态为0的时候获取锁*/
        public boolean tryAcquire(int acquires) {
            if (compareAndSetState(0, 1)) {
                setExclusiveOwnerThread(Thread.currentThread());
                return true;
            }else if(getExclusiveOwnerThread()==Thread.currentThread()){
                setState(getState()+1);
                return  true;
            }
            return false;
        }

        /* 释放锁,将状态设置为0*/
        protected boolean tryRelease(int releases) {
            if(getExclusiveOwnerThread()!=Thread.currentThread()){
                throw new IllegalMonitorStateException();
            }
            if (getState() == 0)
                throw new IllegalMonitorStateException();

            setState(getState()-1);
            if(getState()==0){
                setExclusiveOwnerThread(null);
            }
            return true;
        }

        /* 返回一个Condition,每个condition都包含了一个condition队列*/
        Condition newCondition() {
            return new ConditionObject();
        }
    }

    /* 仅需要将操作代理到Sync上即可*/
    private final Sync sync = new Sync();

    public void lock() {
    	System.out.println(Thread.currentThread().getName()+" ready get lock");
        sync.acquire(1);
        System.out.println(Thread.currentThread().getName()+" already got lock");
    }

    public boolean tryLock() {
        return sync.tryAcquire(1);
    }

    public void unlock() {
    	System.out.println(Thread.currentThread().getName()+" ready release lock");
        sync.release(1);
        System.out.println(Thread.currentThread().getName()+" already released lock");
    }

    public Condition newCondition() {
        return sync.newCondition();
    }

    public boolean isLocked() {
        return sync.isHeldExclusively();
    }

    public boolean hasQueuedThreads() {
        return sync.hasQueuedThreads();
    }

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

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

在AQS的tryAcquire和tryRelease方法中分别对state本身加减

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值