⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 reentrantlock.java

📁 jboss规则引擎
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     *     * <p>In this implementation, as this method is an explicit interruption     * point, preference is     * given to responding to the interrupt over normal or reentrant     * acquisition of the lock.     *     * @throws InterruptedException if the current thread is interrupted     */    public void lockInterruptibly() throws InterruptedException {        this.sync.lockInterruptibly();    }    /**     * Acquires the lock only if it is not held by another thread at the time     * of invocation.     *     * <p>Acquires the lock if it is not held by another thread and     * returns immediately with the value <tt>true</tt>, setting the     * lock hold count to one. Even when this lock has been set to use a     * fair ordering policy, a call to <tt>tryLock()</tt> <em>will</em>     * immediately acquire the lock if it is available, whether or not     * other threads are currently waiting for the lock.     * This &quot;barging&quot; behavior can be useful in certain     * circumstances, even though it breaks fairness. If you want to honor     * the fairness setting for this lock, then use     * {@link #tryLock(long, TimeUnit) tryLock(0, TimeUnit.SECONDS) }     * which is almost equivalent (it also detects interruption).     *     * <p> If the current thread     * already holds this lock then the hold count is incremented by one and     * the method returns <tt>true</tt>.     *     * <p>If the lock is held by another thread then this method will return     * immediately with the value <tt>false</tt>.     *     * @return <tt>true</tt> if the lock was free and was acquired by the     * current thread, or the lock was already held by the current thread; and     * <tt>false</tt> otherwise.     */    public boolean tryLock() {        return this.sync.tryLock();    }    /**     * Attempts to release this lock.     *     * <p>If the current thread is the     * holder of this lock then the hold count is decremented. If the     * hold count is now zero then the lock is released.  If the     * current thread is not the holder of this lock then {@link     * IllegalMonitorStateException} is thrown.     * @throws IllegalMonitorStateException if the current thread does not     * hold this lock.     */    public void unlock() {        this.sync.unlock();    }    /**     * Queries the number of holds on this lock by the current thread.     *     * <p>A thread has a hold on a lock for each lock action that is not     * matched by an unlock action.     *     * <p>The hold count information is typically only used for testing and     * debugging purposes. For example, if a certain section of code should     * not be entered with the lock already held then we can assert that     * fact:     *     * <pre>     * class X {     *   ReentrantLock lock = new ReentrantLock();     *   // ...     *   public void m() {     *     assert lock.getHoldCount() == 0;     *     lock.lock();     *     try {     *       // ... method body     *     } finally {     *       lock.unlock();     *     }     *   }     * }     * </pre>     *     * @return the number of holds on this lock by the current thread,     * or zero if this lock is not held by the current thread.     */    public int getHoldCount() {        return this.sync.getHoldCount();    }    /**     * Queries if this lock is held by the current thread.     *     * <p>Analogous to the {@link Thread#holdsLock} method for built-in     * monitor locks, this method is typically used for debugging and     * testing. For example, a method that should only be called while     * a lock is held can assert that this is the case:     *     * <pre>     * class X {     *   ReentrantLock lock = new ReentrantLock();     *   // ...     *     *   public void m() {     *       assert lock.isHeldByCurrentThread();     *       // ... method body     *   }     * }     * </pre>     *     * <p>It can also be used to ensure that a reentrant lock is used     * in a non-reentrant manner, for example:     *     * <pre>     * class X {     *   ReentrantLock lock = new ReentrantLock();     *   // ...     *     *   public void m() {     *       assert !lock.isHeldByCurrentThread();     *       lock.lock();     *       try {     *           // ... method body     *       } finally {     *           lock.unlock();     *       }     *   }     * }     * </pre>     * @return <tt>true</tt> if current thread holds this lock and     * <tt>false</tt> otherwise.     */    public boolean isHeldByCurrentThread() {        return this.sync.isHeldByCurrentThread();    }    /**     * Queries if this lock is held by any thread. This method is     * designed for use in monitoring of the system state,     * not for synchronization control.     * @return <tt>true</tt> if any thread holds this lock and     * <tt>false</tt> otherwise.     */    public boolean isLocked() {        return this.sync.isLocked();    }    /**     * <tt>null</tt> if not owned. When this method is called by a     * thread that is not the owner, the return value reflects a     * best-effort approximation of current lock status. For example,     * the owner may be momentarily <tt>null</tt> even if there are     * threads trying to acquire the lock but have not yet done so.     * This method is designed to facilitate construction of     * subclasses that provide more extensive lock monitoring     * facilities.     *     * @return the owner, or <tt>null</tt> if not owned     */    protected Thread getOwner() {        return this.sync.getOwner();    }    /**     * Queries whether any threads are waiting to acquire this lock. Note that     * because cancellations may occur at any time, a <tt>true</tt>     * return does not guarantee that any other thread will ever     * acquire this lock.  This method is designed primarily for use in     * monitoring of the system state.     *     * @return true if there may be other threads waiting to acquire     * the lock.     */    public final boolean hasQueuedThreads() {        return this.sync.hasQueuedThreads();    }    /**     * Queries whether the given thread is waiting to acquire this     * lock. Note that because cancellations may occur at any time, a     * <tt>true</tt> return does not guarantee that this thread     * will ever acquire this lock.  This method is designed primarily for use     * in monitoring of the system state.     *     * @param thread the thread     * @return true if the given thread is queued waiting for this lock.     * @throws NullPointerException if thread is null     */    public final boolean hasQueuedThread(final Thread thread) {        return this.sync.isQueued( thread );    }    /**     * Returns an estimate of the number of threads waiting to     * acquire this lock.  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 this.sync.getQueueLength();    }    /**     * Returns a collection containing threads that may be waiting to     * acquire this lock.  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 getQueuedThreads() {        return this.sync.getQueuedThreads();    }    /**     * Returns a string identifying this lock, as well as its lock     * state.  The state, in brackets, includes either the String     * &quot;Unlocked&quot; or the String &quot;Locked by&quot;     * followed by the {@link Thread#getName} of the owning thread.     * @return a string identifying this lock, as well as its lock state.     */    public String toString() {        final Thread o = getOwner();        return super.toString() + ((o == null) ? "[Unlocked]" : "[Locked by thread " + o.getName() + "]");    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -