线程安全锁代码实现

  • 手动实现了一个线程安全的锁,希望对你有所帮助。
import sun.misc.Unsafe;

import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.locks.LockSupport;

public class MyLock {

    private static final Unsafe unsafe = UnSafeInstance.refkectGetUnSafe();
    private static long staticOffset;

    static {
        try {
            staticOffset = unsafe.objectFieldOffset(MyLock.class.getDeclaredField("status"));
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }

    // 状态 0未获取锁,1获取锁
    private volatile int status = 0;

    // 当前持锁线程
    private Thread lockHolder;

    public int getStatus() {
        return status;
    }

    public Thread getLockHolder() {
        return lockHolder;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public void setLockHolder(Thread lockHolder) {
        this.lockHolder = lockHolder;
    }

    // 阻塞队列
    private ConcurrentLinkedQueue<Thread> waiters = new ConcurrentLinkedQueue<>();

    public void lock() {

        // 如果状态为0, 队列为空,且当前线程为等待的第一个线程
        if (acquire()) {
            return;
        }

        Thread currentThread = Thread.currentThread();
        waiters.add(currentThread);

        for (; ; ) {

            if ((currentThread == waiters.peek()) && acquire()) {
                waiters.poll();
                return;
            }

            LockSupport.park(currentThread);
        }
    }

    private boolean acquire() {
        Thread currentThread = Thread.currentThread();

        int c = getStatus();
        if (c == 0) {
            if ((waiters.size() == 0 || currentThread == waiters.peek()) && compareAndSwapStatus(0, 1)) {
                setLockHolder(currentThread);
                return true;
            }
        }

        return false;
    }

    private final boolean compareAndSwapStatus(int except, int update) {
        return unsafe.compareAndSwapInt(this, staticOffset, except, update);
    }

    public void unlock() {
        if (Thread.currentThread() != getLockHolder()) {
            throw new RuntimeException("not current thread");
        }

        int c = getStatus();
        if (compareAndSwapStatus(c, 0)) {
            setLockHolder(null);

            Thread first = waiters.peek();
            if (first != null) {
                LockSupport.unpark(first);
            }
        }
    }

}
import java.lang.reflect.Field;

import sun.misc.Unsafe;

public class UnSafeInstance {

    public static Unsafe refkectGetUnSafe() {
        try {
            Field filed = Unsafe.class.getDeclaredField("theUnsafe");
            filed.setAccessible(true);
            return (Unsafe) filed.get(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值