线程安全锁代码实现

  • 手动实现了一个线程安全的锁,希望对你有所帮助。
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;
    }
}

你可能感兴趣的:(JAVA,多线程)