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

📄 reentrantreadwritelock.java

📁 java1.6众多例子参考
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/* * @(#)ReentrantReadWriteLock.java	1.15 06/03/30 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package java.util.concurrent.locks;import java.util.concurrent.*;import java.util.concurrent.atomic.*;import java.util.*;/** * An implementation of {@link ReadWriteLock} supporting similar * semantics to {@link ReentrantLock}. * <p>This class has the following properties: * * <ul> * <li><b>Acquisition order</b> * * <p> This class does not impose a reader or writer preference * ordering for lock access.  However, it does support an optional * <em>fairness</em> policy. * * <dl> * <dt><b><i>Non-fair mode (default)</i></b> * <dd>When constructed as non-fair (the default), the order of entry * to the read and write lock is unspecified, subject to reentrancy * constraints.  A nonfair lock that is continously contended may * indefinitely postpone one or more reader or writer threads, but * will normally have higher throughput than a fair lock. * <p> * * <dt><b><i>Fair mode</i></b> * <dd> When constructed as fair, threads contend for entry using an * approximately arrival-order policy. When the currently held lock * is released either the longest-waiting single writer thread will * be assigned the write lock, or if there is a group of reader threads * waiting longer than all waiting writer threads, that group will be * assigned the read lock. * * <p>A thread that tries to acquire a fair read lock (non-reentrantly) * will block if either the write lock is held, or there is a waiting * writer thread. The thread will not acquire the read lock until * after the oldest currently waiting writer thread has acquired and * released the write lock. Of course, if a waiting writer abandons * its wait, leaving one or more reader threads as the longest waiters * in the queue with the write lock free, then those readers will be * assigned the read lock. * * <p>A thread that tries to acquire a fair write lock (non-reentrantly) * will block unless both the read lock and write lock are free (which * implies there are no waiting threads).  (Note that the non-blocking * {@link ReadLock#tryLock()} and {@link WriteLock#tryLock()} methods * do not honor this fair setting and will acquire the lock if it is * possible, regardless of waiting threads.) * <p> * </dl> * * <li><b>Reentrancy</b> * * <p>This lock allows both readers and writers to reacquire read or * write locks in the style of a {@link ReentrantLock}. Non-reentrant * readers are not allowed until all write locks held by the writing * thread have been released. * * <p>Additionally, a writer can acquire the read lock, but not * vice-versa.  Among other applications, reentrancy can be useful * when write locks are held during calls or callbacks to methods that * perform reads under read locks.  If a reader tries to acquire the * write lock it will never succeed. * * <li><b>Lock downgrading</b> * <p>Reentrancy also allows downgrading from the write lock to a read lock, * by acquiring the write lock, then the read lock and then releasing the * write lock. However, upgrading from a read lock to the write lock is * <b>not</b> possible. * * <li><b>Interruption of lock acquisition</b> * <p>The read lock and write lock both support interruption during lock * acquisition. * * <li><b>{@link Condition} support</b> * <p>The write lock provides a {@link Condition} implementation that * behaves in the same way, with respect to the write lock, as the * {@link Condition} implementation provided by * {@link ReentrantLock#newCondition} does for {@link ReentrantLock}. * This {@link Condition} can, of course, only be used with the write lock. * * <p>The read lock does not support a {@link Condition} and * {@code readLock().newCondition()} throws * {@code UnsupportedOperationException}. * * <li><b>Instrumentation</b> * <p>This class supports methods to determine whether locks * are held or contended. These methods are designed for monitoring * system state, not for synchronization control. * </ul> * * <p>Serialization of this class behaves in the same way as built-in * locks: a deserialized lock is in the unlocked state, regardless of * its state when serialized. * * <p><b>Sample usages</b>. Here is a code sketch showing how to exploit * reentrancy to perform lock downgrading after updating a cache (exception * handling is elided for simplicity): * <pre> * class CachedData { *   Object data; *   volatile boolean cacheValid; *   ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); * *   void processCachedData() { *     rwl.readLock().lock(); *     if (!cacheValid) { *        // Must release read lock before acquiring write lock *        rwl.readLock().unlock(); *        rwl.writeLock().lock(); *        // Recheck state because another thread might have acquired *        //   write lock and changed state before we did. *        if (!cacheValid) { *          data = ... *          cacheValid = true; *        } *        // Downgrade by acquiring read lock before releasing write lock *        rwl.readLock().lock(); *        rwl.writeLock().unlock(); // Unlock write, still hold read *     } * *     use(data); *     rwl.readLock().unlock(); *   } * } * </pre> * * ReentrantReadWriteLocks can be used to improve concurrency in some * uses of some kinds of Collections. This is typically worthwhile * only when the collections are expected to be large, accessed by * more reader threads than writer threads, and entail operations with * overhead that outweighs synchronization overhead. For example, here * is a class using a TreeMap that is expected to be large and * concurrently accessed. * * <pre>{@code * class RWDictionary { *    private final Map<String, Data> m = new TreeMap<String, Data>(); *    private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); *    private final Lock r = rwl.readLock(); *    private final Lock w = rwl.writeLock(); * *    public Data get(String key) { *        r.lock(); *        try { return m.get(key); } *        finally { r.unlock(); } *    } *    public String[] allKeys() { *        r.lock(); *        try { return m.keySet().toArray(); } *        finally { r.unlock(); } *    } *    public Data put(String key, Data value) { *        w.lock(); *        try { return m.put(key, value); } *        finally { w.unlock(); } *    } *    public void clear() { *        w.lock(); *        try { m.clear(); } *        finally { w.unlock(); } *    } * }}</pre> * * <h3>Implementation Notes</h3> * * <p>This lock supports a maximum of 65535 recursive write locks * and 65535 read locks. Attempts to exceed these limits result in * {@link Error} throws from locking methods. * * @since 1.5 * @author Doug Lea * */public class ReentrantReadWriteLock implements ReadWriteLock, java.io.Serializable  {    private static final long serialVersionUID = -6992448646407690164L;    /** Inner class providing readlock */    private final ReentrantReadWriteLock.ReadLock readerLock;    /** Inner class providing writelock */    private final ReentrantReadWriteLock.WriteLock writerLock;    /** Performs all synchronization mechanics */    private final Sync sync;    /**     * Creates a new {@code ReentrantReadWriteLock} with     * default (nonfair) ordering properties.     */    public ReentrantReadWriteLock() {        this(false);    }    /**     * Creates a new {@code ReentrantReadWriteLock} with     * the given fairness policy.     *     * @param fair {@code true} if this lock should use a fair ordering policy     */    public ReentrantReadWriteLock(boolean fair) {        sync = (fair)? new FairSync() : new NonfairSync();        readerLock = new ReadLock(this);        writerLock = new WriteLock(this);    }    public ReentrantReadWriteLock.WriteLock writeLock() { return writerLock; }    public ReentrantReadWriteLock.ReadLock  readLock()  { return readerLock; }    /**     * Synchronization implementation for ReentrantReadWriteLock.     * Subclassed into fair and nonfair versions.     */    static abstract class Sync extends AbstractQueuedSynchronizer {        private static final long serialVersionUID = 6317671515068378041L;        /*         * Read vs write count extraction constants and functions.         * Lock state is logically divided into two shorts: The lower         * one representing the exclusive (writer) lock hold count,         * and the upper the shared (reader) hold count.         */        static final int SHARED_SHIFT   = 16;        static final int SHARED_UNIT    = (1 << SHARED_SHIFT);        static final int MAX_COUNT      = (1 << SHARED_SHIFT) - 1;        static final int EXCLUSIVE_MASK = (1 << SHARED_SHIFT) - 1;        /** Returns the number of shared holds represented in count  */        static int sharedCount(int c)    { return c >>> SHARED_SHIFT; }        /** Returns the number of exclusive holds represented in count  */        static int exclusiveCount(int c) { return c & EXCLUSIVE_MASK; }        /**         * A counter for per-thread read hold counts.         * Maintained as a ThreadLocal; cached in cachedHoldCounter         */        static final class HoldCounter {            int count;            // Use id, not reference, to avoid garbage retention            final long tid = Thread.currentThread().getId();            /** Decrement if positive; return previous value */            int tryDecrement() {                int c = count;                if (c > 0)                    count = c - 1;                return c;            }        }        /**         * ThreadLocal subclass. Easiest to explicitly define for sake         * of deserialization mechanics.         */        static final class ThreadLocalHoldCounter            extends ThreadLocal<HoldCounter> {            public HoldCounter initialValue() {                return new HoldCounter();            }        }        /**         * The number of read locks held by current thread.         * Initialized only in constructor and readObject.         */        transient ThreadLocalHoldCounter readHolds;        /**         * The hold count of the last thread to successfully acquire         * readLock. This saves ThreadLocal lookup in the common case         * where the next thread to release is the last one to         * acquire. This is non-volatile since it is just used         * as a heuristic, and would be great for threads to cache.         */        transient HoldCounter cachedHoldCounter;        Sync() {            readHolds = new ThreadLocalHoldCounter();            setState(getState()); // ensures visibility of readHolds        }        /*         * Acquires and releases use the same code for fair and         * nonfair locks, but differ in whether/how they allow barging         * when queues are non-empty.         */        /**         * Return true if a reader thread that is otherwise         * eligible for lock should block because of policy         * for overtaking other waiting threads.         */        abstract boolean readerShouldBlock(Thread current);        /**         * Return true if a writer thread that is otherwise         * eligible for lock should block because of policy         * for overtaking other waiting threads.         */        abstract boolean writerShouldBlock(Thread current);        /*         * Note that tryRelease and tryAcquire can be called by         * Conditions. So it is possible that their arguments contain         * both read and write holds that are all released during a         * condition wait and re-established in tryAcquire.         */        protected final boolean tryRelease(int releases) {            int nextc = getState() - releases;            if (Thread.currentThread() != getExclusiveOwnerThread())                throw new IllegalMonitorStateException();            if (exclusiveCount(nextc) == 0) {                setExclusiveOwnerThread(null);                setState(nextc);                return true;            } else {                setState(nextc);                return false;            }        }        protected final boolean tryAcquire(int acquires) {            /*             * Walkthrough:             * 1. if read count nonzero or write count nonzero             *     and owner is a different thread, fail.             * 2. If count would saturate, fail. (This can only             *    happen if count is already nonzero.)             * 3. Otherwise, this thread is eligible for lock if             *    it is either a reentrant acquire or             *    queue policy allows it. If so, update state

⌨️ 快捷键说明

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