zoukankan      html  css  js  c++  java
  • ReentrantLock三大特性

    Doug lea

    可重入 同一线程某方法获取该锁后,如果再另一方法尝试再获取锁,不会被阻塞。 关键字:同一线程 不同方法 阻塞 

    公平 直接通过acquire获取锁(tryacquire,acquirequeue) 公平锁的acuire锁会判断该线程是否是lock的队头

    非公平 先通过compareAndSetState竞争锁,未竞价到锁,再通过acquire获取锁

    Sync接口的不同静态内部类实现   实现了两方法 tryAcquire lock 设计模式中的模板模式

    FairSync

    NonFairSync

        static final class FairSync extends Sync {
            private static final long serialVersionUID = -3000897897090466540L;
    
            final void lock() {
                acquire(1);
            }
    
            /**
             * Fair version of tryAcquire.  Don't grant access unless
             * recursive call or no waiters or is first.
             */
            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;
            }
        }

     公平锁不同于非公平锁的一点是,不会直接通过compareAndSetState去获取锁,而是先通过hasQueuedPredecessors判断该线程是否是队列的队头,否则加入等待队列

            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;
            }
        static final class NonfairSync extends Sync {
            private static final long serialVersionUID = 7316153563782823691L;
    
            /**
             * Performs lock.  Try immediate barge, backing up to normal
             * acquire on failure.
             */
            final void lock() {
           // Sync内部维护一个state变量,如果当前state=0(未被线程占用),将state赋值为1(已被线程占用)
    if (compareAndSetState(0, 1))
            // 当前线程独占该锁 setExclusiveOwnerThread(Thread.currentThread());
    else acquire(1); } protected final boolean tryAcquire(int acquires) { return nonfairTryAcquire(acquires); } }

     AbstractQueuedSynchronizer

        public final void acquire(int arg) {
    //1 尝试获取该锁
    //2 如果没有获取到该锁,则将此线程加入到该lock的队列中去
    if (!tryAcquire(arg) && acquireQueued(addWaiter(Node.EXCLUSIVE), arg)) selfInterrupt(); }

    ReentrantLock Sync

            final boolean nonfairTryAcquire(int acquires) {
                final Thread current = Thread.currentThread();
                int c = getState();
                if (c == 0) {
    // 逻辑 同lock
    if (compareAndSetState(0, acquires)) { setExclusiveOwnerThread(current); return true; } }
    // 如果是当lock的线程是当前线程 强state +1 获得该锁(可重入锁的逻辑)
    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; }

  • 相关阅读:
    python初学第一节课
    关于float类型和u32类型问题
    今天的工作状态,规划未来一段时间内必须完成的事情(Record the working status of today,planning for the next period of time must be completed)
    STM32 硬件I2C初始化 I2C1_GPIO_AF_Config
    C语言编程规范--------12 宏
    C语言编程规范--------11 代码测试、维护
    C语言编程规范--------10 代码编辑、编译、审查
    C语言编程规范--------9 质量保证
    C语言编程规范--------8 效率
    C语言编程规范--------7 可测性
  • 原文地址:https://www.cnblogs.com/lt123/p/12835322.html
Copyright © 2011-2022 走看看