Javaマルチスレッドセマフォソースコード分析

目次

 

前書き

フィールド同期、内部クラス同期

内部クラスFairSync、NonFairSync、2つのコンストラクター

方法4acquireXXX、4 tryAcquireXXX、リリース

方法availablePermits、drainPermits、reducePermits、isFair、hasQueuedThreads、getQueueLength、getQueuedThreads、toString


前書き

package java.util.concurrent;
import java.util.Collection;
import java.util.concurrent.locks.AbstractQueuedSynchronizer;

/**
 * 计数信号量。从概念上讲,一个信号量维护一组许可。
 * 如果有必要,每个acquire阻塞直到一个许可可用,然后获取它。
 * 每次release都会增加一个许可,潜在地释放一个阻塞的获取者。
 * 但是,没有使用实际的permit对象;
 * Semaphore只保存可用数量的计数,并相应地采取行动。
 *
 * <p>信号量通常用于限制不能访问某些(物理或逻辑)资源的线程数量。
 * 例如,这里的一个类使用信号量来控制对项目池的访问:
 * 
 *  <pre> {@code
 * class Pool {
 *   private static final int MAX_AVAILABLE = 100;
 *   private final Semaphore available = new Semaphore(MAX_AVAILABLE, true);
 *
 *   public Object getItem() throws InterruptedException {
 *     available.acquire();
 *     return getNextAvailableItem();
 *   }
 *
 *   public void putItem(Object x) {
 *     if (markAsUnused(x))
 *       available.release();
 *   }
 *
 *   // Not a particularly efficient data structure; just for demo
 *
 *   protected Object[] items = ... whatever kinds of items being managed
 *   protected boolean[] used = new boolean[MAX_AVAILABLE];
 *
 *   protected synchronized Object getNextAvailableItem() {
 *     for (int i = 0; i < MAX_AVAILABLE; ++i) {
 *       if (!used[i]) {
 *          used[i] = true;
 *          return items[i];
 *       }
 *     }
 *     return null; // not reached
 *   }
 *
 *   protected synchronized boolean markAsUnused(Object item) {
 *     for (int i = 0; i < MAX_AVAILABLE; ++i) {
 *       if (item == items[i]) {
 *          if (used[i]) {
 *            used[i] = false;
 *            return true;
 *          } else
 *            return false;
 *       }
 *     }
 *     return false;
 *   }
 * }}</pre>
 *
 * <p>在获得一个项之前,每个线程必须从信号量获得一个许可,保证一个项可以使用。
 * 当线程使用完该对象后,它被返回到池中,并向信号量返回一个许可,允许另一个线程获取该对象。
 * 注意,当调用acquire时,不存在同步锁,因为这会阻止一个项目被返回到池中。
 * 信号量封装了限制访问池所需的同步,与维护池本身的一致性所需的任何同步分开。
 *
 * <p>初始化为1的信号量,并且它最多只有一个允许可用,可以作为互斥锁。
 * 这通常被称为binary semaphore,因为它只有两种状态:一个允许可用,或零允许可用。
 * 当以这种方式使用时,二进制信号量具有这样一个属性(不同于许多java.util.concurrent.locks.Lock实现),
 * 即“锁”可以由所有者以外的线程释放(因为信号量没有所有权的概念)。
 * 这在某些特定的上下文中很有用,比如死锁恢复。
 *
 * <p> 这个类的构造函数可选地接受一个公平性参数。
 * 当设置为false时,该类不保证线程获得许可的顺序。
 * 特别地,barging是允许的,也就是说,线程调用的获取可以在等待的线程之前分配一个许可——逻辑上,新线程将自己放在等待线程队列的头。
 * 
 * 当公平感设置为true时,这些信号量保证调用任何acquire方法的线程被选择以它们调用这些方法的处理顺序获得许可(first-in-first-out;先进先出)。
 * 注意,FIFO排序必须适用于这些方法中的特定内部执行点。
 * 因此,一个线程可能在另一个线程之前调用acquire,但在另一个线程之后到达排序点,从方法返回时也是如此。
 * 还要注意的是,不计时的tryAcquire方法不遵守公平性设置,但会接受任何可用的许可。
 *
 * <p>通常,用于控制资源访问的信号量应该被初始化为公平的,以确保没有线程因为访问资源而饿死。
 * 当使用信号量进行其他类型的同步控制时,非公平排序的吞吐量优势常常超过公平性考虑。
 *
 * <p>这个类还提供了一次获取和释放多个许可证的方便方法。
 * 当这些方法使用时没有设置公平为真,要注意增加的不确定延迟的风险。
 *
 * <p>内存一致性效应:在一个线程中,在调用“release”方法(如release())之前的动作发生在另一个线程中,
 * 在成功调用“acquire”方法(如acquire())之后的动作发生在另一个线程中。
 * 
 * Semaphore在多线程协作中常常用来控制公共资源的访问,限制同时访问数量。
 * 打个比方,Semaphore就像是一个装有令牌(permit)的黑箱子,拿到令牌的人才能去做爱做的事情,
 * 谁都可以从里面拿走若干令牌,谁都可以把新的令牌扔到里面去,但Semaphore从来不记载谁拿走的令牌。
 * 
 * 获取信号量(减小state)
 * Semaphore方法	调用的AQS方法	  是否阻塞	  是否响应中断	  是否超时机制	  返回值及含义
 * 
 * acquire()   	sync.acquireSharedInterruptibly(1)   	✓	✓	-	void
 * acquire(int permits)   	sync.acquireSharedInterruptibly(permits)   	✓	✓	-	void
 * acquireUninterruptibly()   	sync.acquireShared(1)   	✓	-	-	void
 * acquireUninterruptibly(int permits)   	sync.acquireShared(permits)   	✓	-	-	void
 * 
 * tryAcquire(long timeout, TimeUnit unit)   	sync.tryAcquireSharedNanos(1, unit.toNanos(timeout)   )   	✓	✓	✓	boolean  返回时是否获得了锁
 * tryAcquire(int permits, long timeout, TimeUnit unit)   	sync.tryAcquireSharedNanos(permits, unit.toNanos(timeout)   )   	✓	✓	✓	boolean  返回时是否获得了锁
 * tryAcquire()   	sync.nonfairTryAcquireShared(1)    >= 0	-			boolean  返回时是否获得了锁
 * 
 * 上面表格中,所有没有参数的Semaphore方法,实质上都是在获得单位1的信号量。
 * 调用的AQS方法,其实只有三种,acquireShared(共享锁的获取与释放中已经进行了讲解),
 * acquireSharedInterruptibly和tryAcquireSharedNanos(CountDownLatch源码解析中已经进行了讲解)。这三者的区别已经在表格列出。
 * 需要响应中断,方法声明会抛出中断异常。
 * 有超时机制,就需要用返回值区别 获得锁返回 和 超时都没获得到锁 两种情况。
 * 这三个方法都需要调用到AQS子类实现的tryAcquireShared,该方法用来获取共享锁,子类可以将其实现公平锁或是非公平锁。
 * 
 * nonfairTryAcquireShared其实不大应该放在上表里面,因为它根本不是AQS的方法,只是AQS子类的新加方法。
 * 因为它根本没有阻塞等待的过程,只是简单的try一次,成功失败听天由命,所以它根本不会阻塞。
 * 表格中,带permits参数的方法可以获得单位不为1的信号量,但是方法中对permits参数的检查要求是>=0,
 * 也就是说,允许线程获得0单位的信号量,虽然我感觉这样没有任何意义。
 * 
 * 这里我们再复习一下tryAcquireShared返回值的含义:
 * 
 * 如果返回值大于0,说明获取共享锁成功,并且后续获取也可能获取成功。
 * 如果返回值等于0,说明获取共享锁成功,但后续获取可能不会成功。
 * 如果返回值小于0,说明获取共享锁失败。
 * 
 * Semaphore的AQS子类是一个很标准的共享锁实现。
 * 获得信号量 == 减小AQS的state。
 * 释放信号量 == 增加AQS的state。
 * 共享锁的AQS子类实现方法需要自旋,这一点在Semaphore和CountDownLatch都有体现。独占锁的AQS子类实现方法不需要自旋。
 * 获得信号量失败一定是因为信号量已经减到0了,且获得失败就会阻塞。
 *
 * @since 1.5
 * @author Doug Lea
 */
public class Semaphore implements java.io.Serializable 

フィールド同期、内部クラス同期

    private static final long serialVersionUID = -3222578661600680210L;
    /** 所有机制都是通过AbstractQueuedSynchronizer子类实现的 */
    private final Sync sync;

    /**
     * 信号量的同步实现。使用aq状态来表示许可。子类分为公平版和非公平版。
     */
    abstract static class Sync extends AbstractQueuedSynchronizer {
        private static final long serialVersionUID = 1192457210091910933L;

        // 首先看到Sync的构造器,看来参数permits是代表共享锁的数量。
        
        Sync(int permits) {
            setState(permits);
        }

        final int getPermits() {
            return getState();
        }

        /**
         * 方法使用了自旋,这是合理且必要的。共享锁是共享的,自然可能有多个线程正在同时执行上面的代码,
         * 即使失败了也不能退出循环,而是应该失败后再得到当前值,然后再次CAS尝试。
         * 
         * 如果remaining算出来小于0,说明剩余信号量已经不够拿的了,那就直接返回remaining这个负数(表达获取共享锁失败),不做CAS操作。
         * 如果remaining算出来大于等于0,说明剩余信号量够拿的,紧接着如果CAS设置成功,就返回remaining这个大于等于0的数(表达获取共享锁成功)。
         * 这个方法想要退出,只有当前线程拿到了想要数量的信号量,或剩余信号量已经不够拿。
         * 
         * @param acquires
         * @return
         */
        final int nonfairTryAcquireShared(int acquires) {
            for (;;) {
                int available = getState();
                int remaining = available - acquires;
                if (remaining < 0 ||
                    compareAndSetState(available, remaining))
                    return remaining;
            }
        }

        /**
         *释放信号量就不用区别什么公平不公平了。
         *考虑多线程,使用自旋保证releases单位的信号量能够释放到位。
         *只有CAS设置成功,或溢出int型的范围,才能退出这个循环。
         */
        protected final boolean tryReleaseShared(int releases) {
            for (;;) {
                int current = getState();
                int next = current + releases;
                if (next < current) // overflow
                    throw new Error("Maximum permit count exceeded");
                if (compareAndSetState(current, next))
                    return true;
            }
        }

        /**
         * 这个方法的作用也是获得信号量,只不过这个函数相比nonfairTryAcquireShared的实现,它允许改变后的信号量是负数。
         * 自旋的过程。想要退出函数,只有CAS操作成功或者向下溢出了。
         * if (next > current)分支进入的原因只能是int型变量向下溢出,因为reductions被保证是一个>=0的数。
         * 是protected方法,用起来不是那么方便。
         * 
         * @param reductions
         */
        final void reducePermits(int reductions) {
            for (;;) {
                int current = getState();
                int next = current - reductions;
                if (next > current) // underflow
                    throw new Error("Permit count underflow");
                if (compareAndSetState(current, next))
                    return;
            }
        }

        /**
         * 自旋的过程,直到信号量被清空为0。
         * @return
         */
        final int drainPermits() {
            for (;;) {
                int current = getState();
                if (current == 0 || compareAndSetState(current, 0))
                    return current;
            }
        }
    }

内部クラスFairSync、NonFairSync、2つのコンストラクター


    /**
     * NonFair version
     */
    static final class NonfairSync extends Sync {
        private static final long serialVersionUID = -2694183684443567898L;

        NonfairSync(int permits) {
            super(permits);
        }

        protected int tryAcquireShared(int acquires) {
            return nonfairTryAcquireShared(acquires);
        }
    }

    /**
     * Fair version
     */
    static final class FairSync extends Sync {
        private static final long serialVersionUID = 2014338818796000944L;

        FairSync(int permits) {
            super(permits);
        }

        /**
         * 观察tryAcquireShared的公平和非公平锁的逻辑,
         * 发现区别只是 公平锁里面每次循环都会判断hasQueuedPredecessors()的返回值。
         * 
         * 如果有那同步队列中的节点属于是排在当前线程之前的,所以只好直接返回-1。
         */
        protected int tryAcquireShared(int acquires) {
            for (;;) {
                if (hasQueuedPredecessors())
                    return -1;
                int available = getState();
                int remaining = available - acquires;
                if (remaining < 0 ||
                    compareAndSetState(available, remaining))
                    return remaining;
            }
        }
    }

    /**
     * 创建一个具有给定许可证数量和不公平公平设置的信号量。
     * 
     * 默认构造器使用非公平锁,可以通过参数fair来得到公平锁。参数permits最终会赋值给AQS的state成员。
     *
     * @param permits the initial number of permits available.
     *        This value may be negative, in which case releases
     *        must occur before any acquires will be granted.
     */
    public Semaphore(int permits) {
        sync = new NonfairSync(permits);
    }

    /**
     * 创建一个具有给定许可数量和给定公平设置的信号量。
     *
     * @param permits the initial number of permits available.
     *        This value may be negative, in which case releases
     *        must occur before any acquires will be granted.
     * @param fair {@code true} if this semaphore will guarantee
     *        first-in first-out granting of permits under contention,
     *        else {@code false}
     */
    public Semaphore(int permits, boolean fair) {
        sync = fair ? new FairSync(permits) : new NonfairSync(permits);
    }

方法4acquireXXX、4 tryAcquireXXX、リリース


    /**
     * 从这个信号量获得一个许可,阻塞直到有一个可用,或者线程被中断。
     *
     * <p>获得一个许可证,如果有,立即返回,减少一个许可证的数量。
     *
     * <p>如果没有可用的许可,那么当前线程就会因为线程调度的目的而被禁用,并且处于休眠状态,直到以下两件事情发生:
     * <ul>
     * <li>其他线程调用这个信号量的release方法,当前线程将被分配一个许可证;或
     * <li>其他线程中断当前线程。
     * </ul>
     *
     * <p>如果当前线程:
     * <ul>
     * <li>在进入该方法时设置中断状态;或
     * <li>在等待许可时被中断,
     * </ul>
     * 然后抛出InterruptedException,并清除当前线程的中断状态。
     *
     * @throws InterruptedException if the current thread is interrupted
     */
    public void acquire() throws InterruptedException {
        sync.acquireSharedInterruptibly(1);
    }

    /**
     * 从这个信号量获得一个许可,阻塞直到一个可用。
     *
     * <p>获得一个许可证,如果有,立即返回,减少一个许可证的数量。
     *
     * <p>如果没有可用的许可,那么当前线程就会因为线程调度的目的而被禁用,并且处于休眠状态,
     * 直到其他线程调用这个信号量的release方法,并且当前线程下一个被分配一个许可。
     *
     * <p>如果当前线程在等待许可时被中断,那么它将继续等待,
     * 但是与没有发生中断时收到许可的时间相比,给线程分配许可的时间可能会发生变化。
     * 当线程确实从这个方法返回时,它的interrupt status将被设置。
     */
    public void acquireUninterruptibly() {
        sync.acquireShared(1);
    }

    /**
     * 只有在调用时该信号量是可用的,才会获得该信号量的许可。
     *
     * <p>获得一个许可(如果一个许可是可用的),并立即返回,值为true,将可用许可的数量减少一。
     *
     * <p>如果没有可用的许可,则此方法将立即返回值为false。
     *
     * <p>即使这个信号量被设置为使用公平排序策略,
     * 调用tryAcquire()将立即获得一个许可,如果一个许可是可用的,无论其他线程是否正在等待。
     * 这种“冲撞”行为在某些情况下是有用的,即使它破坏了公平。
     * 如果你想遵守公平性设置,那么使用tryAcquire(0, TimeUnit.SECONDS),这几乎是等价的(它也检测中断)。
     * 
     * 上面表格里,一直没有好好讲tryAcquire,因为它在里面属于一个异类,没有阻塞等待的过程。
     * 
     * 而nonfairTryAcquireShared的实现也看过了,里面根本没有调用过LockSupport.park,确实没有阻塞。
     * 不要被自旋所迷惑,自旋也不是阻塞,而且这个自旋过程一般情形下很快就会结束。
     * 
     * 简而言之,Semaphore#tryAcquire的作用就是尝试 一次性的、非公平的 获得锁动作。
     * 注意这种一次性动作一定要是非公平实现的,不然大部分情况下(同步队列中只要有一个线程在等待),这种一次性动作肯定不能成功。
     * 这也是为什么要把非公平实现放到NonfairSync和FairSync的父类里的一个公共方法里。
     * 
     * 注意,返回值进行了处理,如果获得共享锁成功(nonfairTryAcquireShared返回值>=0),返回true;
     * 如果获得共享锁失败(nonfairTryAcquireShared返回值<0),返回false。
     *
     * @return {@code true} if a permit was acquired and {@code false}
     *         otherwise
     */
    public boolean tryAcquire() {
        return sync.nonfairTryAcquireShared(1) >= 0;
    }

    /**
     * Acquires a permit from this semaphore, if one becomes available
     * within the given waiting time and the current thread has not
     * been {@linkplain Thread#interrupt interrupted}.
     *
     * <p>Acquires a permit, if one is available and returns immediately,
     * with the value {@code true},
     * reducing the number of available permits by one.
     *
     * <p>If no permit is available then the current thread becomes
     * disabled for thread scheduling purposes and lies dormant until
     * one of three things happens:
     * <ul>
     * <li>Some other thread invokes the {@link #release} method for this
     * semaphore and the current thread is next to be assigned a permit; or
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
     * the current thread; or
     * <li>The specified waiting time elapses.
     * </ul>
     *
     * <p>If a permit is acquired then the value {@code true} is returned.
     *
     * <p>If the current thread:
     * <ul>
     * <li>has its interrupted status set on entry to this method; or
     * <li>is {@linkplain Thread#interrupt interrupted} while waiting
     * to acquire a permit,
     * </ul>
     * then {@link InterruptedException} is thrown and the current thread's
     * interrupted status is cleared.
     *
     * <p>If the specified waiting time elapses then the value {@code false}
     * is returned.  If the time is less than or equal to zero, the method
     * will not wait at all.
     *
     * @param timeout the maximum time to wait for a permit
     * @param unit the time unit of the {@code timeout} argument
     * @return {@code true} if a permit was acquired and {@code false}
     *         if the waiting time elapsed before a permit was acquired
     * @throws InterruptedException if the current thread is interrupted
     */
    public boolean tryAcquire(long timeout, TimeUnit unit)
        throws InterruptedException {
        return sync.tryAcquireSharedNanos(1, unit.toNanos(timeout));
    }

    /**
     * 释放一个许可,并将其返回给信号量。
     *
     * <p>释放一个许可证,增加一个可用许可证的数量。
     * 如果有任何线程试图获得一个许可,那么将选择一个线程并给予刚刚释放的许可。
     * 为线程调度目的启用了该线程(re)。
     *
     * <p>没有要求释放许可的线程必须通过调用acquire获得该许可。
     * 正确使用信号量是通过应用程序的编程约定来建立的。
     */
    public void release() {
        sync.releaseShared(1);
    }

    /**
     * Acquires the given number of permits from this semaphore,
     * blocking until all are available,
     * or the thread is {@linkplain Thread#interrupt interrupted}.
     *
     * <p>Acquires the given number of permits, if they are available,
     * and returns immediately, reducing the number of available permits
     * by the given amount.
     *
     * <p>If insufficient permits are available then the current thread becomes
     * disabled for thread scheduling purposes and lies dormant until
     * one of two things happens:
     * <ul>
     * <li>Some other thread invokes one of the {@link #release() release}
     * methods for this semaphore, the current thread is next to be assigned
     * permits and the number of available permits satisfies this request; or
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
     * the current thread.
     * </ul>
     *
     * <p>If the current thread:
     * <ul>
     * <li>has its interrupted status set on entry to this method; or
     * <li>is {@linkplain Thread#interrupt interrupted} while waiting
     * for a permit,
     * </ul>
     * then {@link InterruptedException} is thrown and the current thread's
     * interrupted status is cleared.
     * Any permits that were to be assigned to this thread are instead
     * assigned to other threads trying to acquire permits, as if
     * permits had been made available by a call to {@link #release()}.
     *
     * @param permits the number of permits to acquire
     * @throws InterruptedException if the current thread is interrupted
     * @throws IllegalArgumentException if {@code permits} is negative
     */
    public void acquire(int permits) throws InterruptedException {
        if (permits < 0) throw new IllegalArgumentException();
        sync.acquireSharedInterruptibly(permits);
    }

    /**
     * Acquires the given number of permits from this semaphore,
     * blocking until all are available.
     *
     * <p>Acquires the given number of permits, if they are available,
     * and returns immediately, reducing the number of available permits
     * by the given amount.
     *
     * <p>If insufficient permits are available then the current thread becomes
     * disabled for thread scheduling purposes and lies dormant until
     * some other thread invokes one of the {@link #release() release}
     * methods for this semaphore, the current thread is next to be assigned
     * permits and the number of available permits satisfies this request.
     *
     * <p>If the current thread is {@linkplain Thread#interrupt interrupted}
     * while waiting for permits then it will continue to wait and its
     * position in the queue is not affected.  When the thread does return
     * from this method its interrupt status will be set.
     *
     * @param permits the number of permits to acquire
     * @throws IllegalArgumentException if {@code permits} is negative
     */
    public void acquireUninterruptibly(int permits) {
        if (permits < 0) throw new IllegalArgumentException();
        sync.acquireShared(permits);
    }

    /**
     * Acquires the given number of permits from this semaphore, only
     * if all are available at the time of invocation.
     *
     * <p>Acquires the given number of permits, if they are available, and
     * returns immediately, with the value {@code true},
     * reducing the number of available permits by the given amount.
     *
     * <p>If insufficient permits are available then this method will return
     * immediately with the value {@code false} and the number of available
     * permits is unchanged.
     *
     * <p>Even when this semaphore has been set to use a fair ordering
     * policy, a call to {@code tryAcquire} <em>will</em>
     * immediately acquire a permit if one is available, whether or
     * not other threads are currently waiting.  This
     * &quot;barging&quot; behavior can be useful in certain
     * circumstances, even though it breaks fairness. If you want to
     * honor the fairness setting, then use {@link #tryAcquire(int,
     * long, TimeUnit) tryAcquire(permits, 0, TimeUnit.SECONDS) }
     * which is almost equivalent (it also detects interruption).
     *
     * @param permits the number of permits to acquire
     * @return {@code true} if the permits were acquired and
     *         {@code false} otherwise
     * @throws IllegalArgumentException if {@code permits} is negative
     */
    public boolean tryAcquire(int permits) {
        if (permits < 0) throw new IllegalArgumentException();
        return sync.nonfairTryAcquireShared(permits) >= 0;
    }

    /**
     * Acquires the given number of permits from this semaphore, if all
     * become available within the given waiting time and the current
     * thread has not been {@linkplain Thread#interrupt interrupted}.
     *
     * <p>Acquires the given number of permits, if they are available and
     * returns immediately, with the value {@code true},
     * reducing the number of available permits by the given amount.
     *
     * <p>If insufficient permits are available then
     * the current thread becomes disabled for thread scheduling
     * purposes and lies dormant until one of three things happens:
     * <ul>
     * <li>Some other thread invokes one of the {@link #release() release}
     * methods for this semaphore, the current thread is next to be assigned
     * permits and the number of available permits satisfies this request; or
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
     * the current thread; or
     * <li>The specified waiting time elapses.
     * </ul>
     *
     * <p>If the permits are acquired then the value {@code true} is returned.
     *
     * <p>If the current thread:
     * <ul>
     * <li>has its interrupted status set on entry to this method; or
     * <li>is {@linkplain Thread#interrupt interrupted} while waiting
     * to acquire the permits,
     * </ul>
     * then {@link InterruptedException} is thrown and the current thread's
     * interrupted status is cleared.
     * Any permits that were to be assigned to this thread, are instead
     * assigned to other threads trying to acquire permits, as if
     * the permits had been made available by a call to {@link #release()}.
     *
     * <p>If the specified waiting time elapses then the value {@code false}
     * is returned.  If the time is less than or equal to zero, the method
     * will not wait at all.  Any permits that were to be assigned to this
     * thread, are instead assigned to other threads trying to acquire
     * permits, as if the permits had been made available by a call to
     * {@link #release()}.
     *
     * @param permits the number of permits to acquire
     * @param timeout the maximum time to wait for the permits
     * @param unit the time unit of the {@code timeout} argument
     * @return {@code true} if all permits were acquired and {@code false}
     *         if the waiting time elapsed before all permits were acquired
     * @throws InterruptedException if the current thread is interrupted
     * @throws IllegalArgumentException if {@code permits} is negative
     */
    public boolean tryAcquire(int permits, long timeout, TimeUnit unit)
        throws InterruptedException {
        if (permits < 0) throw new IllegalArgumentException();
        return sync.tryAcquireSharedNanos(permits, unit.toNanos(timeout));
    }

    /**
     * Releases the given number of permits, returning them to the semaphore.
     *
     * <p>Releases the given number of permits, increasing the number of
     * available permits by that amount.
     * If any threads are trying to acquire permits, then one
     * is selected and given the permits that were just released.
     * If the number of available permits satisfies that thread's request
     * then that thread is (re)enabled for thread scheduling purposes;
     * otherwise the thread will wait until sufficient permits are available.
     * If there are still permits available
     * after this thread's request has been satisfied, then those permits
     * are assigned in turn to other threads trying to acquire permits.
     *
     * <p>There is no requirement that a thread that releases a permit must
     * have acquired that permit by calling {@link Semaphore#acquire acquire}.
     * Correct usage of a semaphore is established by programming convention
     * in the application.
     *
     * @param permits the number of permits to release
     * @throws IllegalArgumentException if {@code permits} is negative
     */
    public void release(int permits) {
        if (permits < 0) throw new IllegalArgumentException();
        sync.releaseShared(permits);
    }

方法availablePermits、drainPermits、reducePermits、isFair、hasQueuedThreads、getQueueLength、getQueuedThreads、toString


    /**
     * 返回该信号量中可用的当前许可数量。
     *
     * <p>此方法通常用于调试和测试目的。
     *
     * @return the number of permits available in this semaphore
     */
    public int availablePermits() {
        return sync.getPermits();
    }

    /**
     * 获取并返回所有可立即获得的许可。
     *
     * @return the number of permits acquired
     */
    public int drainPermits() {
        return sync.drainPermits();
    }

    /**
     * 按指示减少可用许可证的数量。
     * 在使用semaphores跟踪不可用资源的子类中,此方法非常有用。
     * 这种方法不同于获取,因为它不会阻塞等待许可变得可用。
     *
     * @param reduction the number of permits to remove
     * @throws IllegalArgumentException if {@code reduction} is negative
     */
    protected void reducePermits(int reduction) {
        if (reduction < 0) throw new IllegalArgumentException();
        sync.reducePermits(reduction);
    }

    /**
     * 如果该信号量的公平性设置为true则返回true。
     *
     * @return {@code true} if this semaphore has fairness set true
     */
    public boolean isFair() {
        return sync instanceof FairSync;
    }

    /**
     * 查询是否有等待获取的线程。
     * 注意,因为取消可能在任何时候发生,返回true并不保证任何其他线程会获得。
     * 该方法主要用于监控系统状态。
     *
     * @return {@code true} if there may be other threads waiting to
     *         acquire the lock
     */
    public final boolean hasQueuedThreads() {
        return sync.hasQueuedThreads();
    }

    /**
     * Returns an estimate of the number of threads waiting to acquire.
     * The value is only an estimate because the number of threads may
     * change dynamically while this method traverses internal data
     * structures.  This method is designed for use in monitoring of the
     * system state, not for synchronization control.
     *
     * @return the estimated number of threads waiting for this lock
     */
    public final int getQueueLength() {
        return sync.getQueueLength();
    }

    /**
     * Returns a collection containing threads that may be waiting to acquire.
     * Because the actual set of threads may change dynamically while
     * constructing this result, the returned collection is only a best-effort
     * estimate.  The elements of the returned collection are in no particular
     * order.  This method is designed to facilitate construction of
     * subclasses that provide more extensive monitoring facilities.
     *
     * @return the collection of threads
     */
    protected Collection<Thread> getQueuedThreads() {
        return sync.getQueuedThreads();
    }

    /**
     * Returns a string identifying this semaphore, as well as its state.
     * The state, in brackets, includes the String {@code "Permits ="}
     * followed by the number of permits.
     *
     * @return a string identifying this semaphore, as well as its state
     */
    public String toString() {
        return super.toString() + "[Permits = " + sync.getPermits() + "]";
    }

 

おすすめ

転載: blog.csdn.net/xushiyu1996818/article/details/115277861
おすすめ