美文网首页
AbstractQueuedSynchronizer(AQS)整

AbstractQueuedSynchronizer(AQS)整

作者: SnailFast | 来源:发表于2019-09-16 22:01 被阅读0次

    成员变量

        private

             transient volatile Nodehead

             transient volatile Nodetail

             volatile intstate

             static final Unsafeunsafe = Unsafe.getUnsafe()

             static final longstateOffset

             static final longheadOffset

             static final longtailOffset

             static final longwaitStatusOffset

             static final longnextOffset

        public

             class ConditionObject implements Condition

                private static final intREINTERRUPT = 1

                private static final intTHROW_IE    = -1

                private transient NodefirstWaiter

                private transient NodelastWaiter

                public ConditionObject()

                private Node addConditionWaiter()

                private void doSignal(Node first)

                private void doSignalAll(Node first)

                private void unlinkCancelledWaiters()

                public final void signal()

                public final void signalAll()

                public final void awaitUninterruptibly()

                private int checkInterruptWhileWaiting(Node node)

                private void reportInterruptAfterWait(int interruptMode)

                public final void await() 

                public final long awaitNanos(long nanosTimeout)

                public final boolean awaitUntil(Date deadline)

                public final boolean await(long time,TimeUnit unit)

                final boolean isOwnedBy(AbstractQueuedSynchronizer sync)

                protected final boolean hasWaiters()

                protected final int getWaitQueueLength()

                protected final Collection<Thread> getWaitingThreads()

        default

            static final longspinForTimeoutThreshold = 1000L

            static final class Node

                static final NodeSHARED = new Node()

                static final NodeEXCLUSIVE = null

                static final intCANCELLED = 1

                static final intSIGNAL    = -1

                static final intCONDITION = -2

                static final intPROPAGATE = -3

                volatile intwaitStatus

                volatile Nodeprev

                volatile Nodenext    

                volatile Threadthread

                Node nextWaiter

                final boolean isShared()

                final Node predecessor()

                Node()

                Node(Thread thread, Node mode)

                Node(Thread thread, int waitStatus)

    方法

        private

            Node enq(final Node node)

            Node addWaiter(Node mode)

            void setHead(Node node)

             void unparkSuccessor(Node node)

            void doReleaseShared()

            static boolean shouldParkAfterFailedAcquire()

            final boolean parkAndCheckInterrupt()

            final boolean acquireQueued(final Node node, int arg)

            void doAcquireInterruptibly(int arg)

            boolean doAcquireNanos(int arg, long nanosTimeout)

            void doAcquireShared(int arg)

            void doAcquireSharedInterruptibly(int arg)

            boolean doAcquireSharedNanos(int arg, long nanosTimeout)

            Thread fullGetFirstQueuedThread()

            boolean findNodeFromTail(Node node)

            final boolean compareAndSetHead(Node update)

            final boolean compareAndSetTail(Node expect, Node update)

            static final boolean compareAndSetWaitStatus(Node node,int expect, int update)

            static final boolean compareAndSetNext(Node node,Node expect,Node update)

        protected

            AbstractQueuedSynchronizer()

            final int getState()

            final void setState(int newState)

            final boolean compareAndSetState(int expect, int update)

            void setHeadAndPropagate(Node node, int propagate)

            void cancelAcquire(Node node)

            boolean tryAcquire(int arg)需要子类重写

            boolean tryRelease(int arg)需要子类重写

            int tryAcquireShared(int arg)需要子类重写

            boolean tryReleaseShared(int arg)需要子类重写

            boolean isHeldExclusively()需要子类重写

        public

            final void acquire(int arg)

            final void acquireInterruptibly(int arg)

            final boolean tryAcquireNanos(int arg, long nanosTimeout)

            final boolean release(int arg)

            final void acquireShared(int arg)

            final void acquireSharedInterruptibly(int arg)

            final boolean tryAcquireSharedNanos(int arg, long nanosTimeout)

            final boolean releaseShared(int arg)

            final boolean hasQueuedThreads()

            final boolean hasContended()

            final Thread getFirstQueuedThread()

            final boolean isQueued(Thread thread)

            final boolean hasQueuedPredecessors()

            final int getQueueLength()

            final Collection<Thread> getQueuedThreads()

            final Collection<Thread> getExclusiveQueuedThreads()

            final Collection<Thread> getSharedQueuedThreads()

            String toString()

            final boolean owns(ConditionObject condition)

            final boolean hasWaiters(ConditionObject condition)

            final int getWaitQueueLength(ConditionObject condition)

            final Collection<Thread> getWaitingThreads(ConditionObject condition)

        default

            static void selfInterrupt()

            final boolean apparentlyFirstQueuedIsExclusive()

            final boolean isOnSyncQueue(Node node)

            final boolean transferForSignal(Node node)

            final boolean transferAfterCancelledWait(Node node)

            final int fullyRelease(Node node)

    从父类继承

            private transient Thread exclusiveOwnerThread

            protected final void setExclusiveOwnerThread(Thread thread) 

            protected final Thread getExclusiveOwnerThread()

    总结:

        AQS类虽然是个抽象类,但是没有抽象方法,只有五个会抛UnsupportedOperationException异常的方法,如果使用的话需要子类重写。tryAcquire和tryRelease代表独占锁的加锁和解锁实现,tryAcquireShared和tryReleaseShared代表共享锁的加锁和解锁实现。

        虽然ReentrantLock、CountDownLatch、Semaphore和ReentrantReadWriteLock都是基于AQS实现的同步原语,但是 AQS代码中没有lock字样,都是在子类中实现的语义。

        AQS的实现类都是通过对state状态变量的控制来标识是否成功抢到锁,成功抢到锁则tryAcquire或者tryAcquireShared返回true。释放锁的过程也是控制状态变量,控制完之后,如果state变为0,则表示锁被释放来,tryRelease或者tryReleaseShared返回true;state不为0,则tryRelease或者tryReleaseShared返回false。

    相关文章

      网友评论

          本文标题:AbstractQueuedSynchronizer(AQS)整

          本文链接:https://www.haomeiwen.com/subject/jcfxyctx.html