河北王校长源码之AQS

AQS

AQS可以重写的方法

        // 排他锁
        protected boolean tryAcquire(int arg) {
            return super.tryAcquire(arg);
        }
        protected boolean tryRelease(int arg) {
            return super.tryRelease(arg);
        }
        // 共享锁
        protected int tryAcquireShared(int arg) {
            return super.tryAcquireShared(arg);
        }
        protected boolean tryReleaseShared(int arg) {
            return super.tryReleaseShared(arg);
        }
        // 线程执行情况
        protected boolean isHeldExclusively() {
            return super.isHeldExclusively();
        }

lock可以重写的方法

    public void lock() {}
    public void lockInterruptibly() throws InterruptedException {}
    public boolean tryLock() {
        return false;
    }
    public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
        return false;
    }
    public void unlock() {}
    public Condition newCondition() {
        return null;
    }

方法

加锁方法

    public void lock() {
        sync.lock();
    }
    static final class NonfairSync extends Sync {
        private static final long serialVersionUID = 7316153563782823691L;

        final void lock() {
            // 设置state成功,将当前线程设置为独占线程
            if (compareAndSetState(0, 1))
                setExclusiveOwnerThread(Thread.currentThread());
            else
                // 进入获取流程
                acquire(1);
        }

        protected final boolean tryAcquire(int acquires) {
            return nonfairTryAcquire(acquires);
        }
    }
    static final class FairSync extends Sync {
        private static final long serialVersionUID = -3000897897090466540L;

        final void lock() {
            // 获取流程
            acquire(1);
        }

        protected final boolean tryAcquire(int acquires) {
            final Thread current = Thread.currentThread();
            int c = getState();
            // 
            if (c == 0) {
                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;
        }
    }
    protected final void setExclusiveOwnerThread(Thread thread) {
        exclusiveOwnerThread = thread;
    }
    public final void acquire(int arg) {
        if (!tryAcquire(arg) && acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
            selfInterrupt();
    }
    protected final int getState() {
        return state;
    }
    abstract static class Sync extends AbstractQueuedSynchronizer {
        final boolean nonfairTryAcquire(int acquires) {
            final Thread current = Thread.currentThread();
            int c = getState();
            if (c == 0) {
                if (compareAndSetState(0, acquires)) {
                    setExclusiveOwnerThread(current);
                    return true;
                }
            }
            else if (current == getExclusiveOwnerThread()) {
                int nextc = c + acquires;
                if (nextc < 0) // overflow
                    throw new Error("Maximum lock count exceeded");
                setState(nextc);
                return true;
            }
            return false;
        }
    }
posted @ 2024-03-14 20:09  Faetbwac  阅读(5)  评论(0编辑  收藏  举报