利用AtomicInteger完成一个可重入锁(含阻塞队列)

package com.wyh.lock.Test;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.LockSupport;

@Slf4j(topic = "c.FairyLock")
public class FairyLock {
    /**
     * 1.默认线程状态
     */
    private AtomicInteger lockState = new AtomicInteger(0);
    /**
     * 2.持有锁对象
     */
    private Thread ownerLockThread;
    /**
     * 3.记录重入次数
     */
    private volatile int recursions;
    /**
     * 4.没有获取锁的线程
     */
    private LinkedBlockingDeque<Thread> noLockThreadList = new LinkedBlockingDeque<>();


    /**
     * 5.加锁
     */
    public void lock() throws InterruptedException {
        /**
         * 4.1当前持锁对象不为空,为当前线程+1
         */
        if (ownerLockThread != null && ownerLockThread == Thread.currentThread()) {
            recursions++;
            return;
        }
        /**
         * 4.2第一次获取锁
         * 如果cas次数>10次加入阻塞队列
         */
        int spinCount=0;
        for (; ; ) {
            //4.3cas次数过多,阻塞——>该过程为重型锁
            if(spinCount>10){
                noLockThreadList.offer(Thread.currentThread());
                LockSupport.park();
            }
            //4.4轻量型锁过程
            if (lockState.compareAndSet(0, 1)) {
                ownerLockThread = Thread.currentThread();
                recursions++;
                log.debug("{}获取到锁...", ownerLockThread);
                return;
            }
            //4.4自旋次数+1
            Thread.sleep(100);
            spinCount++;
        }
    }

    /**
     * 5.解锁
     */
    public void unlock() throws Exception {
        /**
         *  5.1如果不是当前线程获取到的锁,抛出异常
         */
        if (ownerLockThread != Thread.currentThread()) {
            throw new Exception("当前线程没有获取到锁,无法进行释放");
        }
        /**
         *  5.2获取到锁,可重入次数-1
         */
        recursions--;
        if (recursions == 0) {
            for (; ; ) {
                if (lockState.compareAndSet(1, 0)) {
                    //唤醒正在阻塞的线程,阻塞队列的线程进入获取锁状态
                    log.debug("开始唤醒阻塞队列中的线程");
                    notifyNotLockThread();
                    return;
                }
            }
        }
    }

    /**
     * 6.唤醒阻塞队列中的线程(非公平)
     */
    private void notifyNotLockThread() {
        for (Thread thread : noLockThreadList) {
            log.debug("开始唤醒阻塞中的线程{}",Thread.currentThread());
            LockSupport.unpark(thread);
        }
    }

    public static void test() throws Exception {
        FairyLock wu_lock = new FairyLock();
        try {
            log.debug("{}开始获取锁",Thread.currentThread());
            wu_lock.lock();//主线程获取到锁

            //7.1子线程操作
            new Thread(()->{
                log.debug("{}开始获取锁",Thread.currentThread());
                try {
                    wu_lock.lock();//这里被阻塞住了
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                log.debug("{}获取锁结束",Thread.currentThread());
            },"t1").start();

            //7.2主线程操作
            log.debug("主线程开始模拟...");
            Thread.sleep(500);
            log.debug("主线程模拟完毕...");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //7.3解锁
            wu_lock.unlock();
        }
    }

    public static void main(String[] args) throws Exception {
        test();
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Fairy要carry

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值