src/java.base/share/classes/java/util/concurrent/locks/ReadWriteLock.java
author dl
Thu, 02 May 2019 06:33:28 -0700
changeset 54685 e1bec7613945
parent 47216 71c04702a3d5
permissions -rw-r--r--
8220248: fix headings in java.util.concurrent Reviewed-by: martin, jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * This file is available under and governed by the GNU General Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * License version 2 only, as published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * However, the following notice accompanied the original version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Expert Group and released to the public domain, as explained at
9242
ef138d47df58 7034657: Update Creative Commons license URL in legal notices
dl
parents: 5506
diff changeset
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
package java.util.concurrent.locks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 9242
diff changeset
    39
 * A {@code ReadWriteLock} maintains a pair of associated {@link
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Lock locks}, one for read-only operations and one for writing.
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
    41
 * The {@linkplain #readLock read lock} may be held simultaneously
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
    42
 * by multiple reader threads, so long as there are no writers.
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
    43
 * The {@linkplain #writeLock write lock} is exclusive.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 9242
diff changeset
    45
 * <p>All {@code ReadWriteLock} implementations must guarantee that
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 9242
diff changeset
    46
 * the memory synchronization effects of {@code writeLock} operations
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * (as specified in the {@link Lock} interface) also hold with respect
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 9242
diff changeset
    48
 * to the associated {@code readLock}. That is, a thread successfully
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * acquiring the read lock will see all updates made upon previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * release of the write lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p>A read-write lock allows for a greater level of concurrency in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * accessing shared data than that permitted by a mutual exclusion lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * It exploits the fact that while only a single thread at a time (a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <em>writer</em> thread) can modify the shared data, in many cases any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * number of threads can concurrently read the data (hence <em>reader</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * threads).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * In theory, the increase in concurrency permitted by the use of a read-write
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * lock will lead to performance improvements over the use of a mutual
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * exclusion lock. In practice this increase in concurrency will only be fully
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * realized on a multi-processor, and then only if the access patterns for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * the shared data are suitable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <p>Whether or not a read-write lock will improve performance over the use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * of a mutual exclusion lock depends on the frequency that the data is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * read compared to being modified, the duration of the read and write
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * operations, and the contention for the data - that is, the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * threads that will try to read or write the data at the same time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * For example, a collection that is initially populated with data and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * thereafter infrequently modified, while being frequently searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * (such as a directory of some kind) is an ideal candidate for the use of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * a read-write lock. However, if updates become frequent then the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * spends most of its time being exclusively locked and there is little, if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * increase in concurrency. Further, if the read operations are too short
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * the overhead of the read-write lock implementation (which is inherently
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * more complex than a mutual exclusion lock) can dominate the execution
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * cost, particularly as many read-write lock implementations still serialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * all threads through a small section of code. Ultimately, only profiling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * and measurement will establish whether the use of a read-write lock is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * suitable for your application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <p>Although the basic operation of a read-write lock is straight-forward,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * there are many policy decisions that an implementation must make, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * may affect the effectiveness of the read-write lock in a given application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * Examples of these policies include:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <li>Determining whether to grant the read lock or the write lock, when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * both readers and writers are waiting, at the time that a writer releases
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * the write lock. Writer preference is common, as writes are expected to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * short and infrequent. Reader preference is less common as it can lead to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * lengthy delays for a write if the readers are frequent and long-lived as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * expected. Fair, or &quot;in-order&quot; implementations are also possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <li>Determining whether readers that request the read lock while a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * reader is active and a writer is waiting, are granted the read lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * Preference to the reader can delay the writer indefinitely, while
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * preference to the writer can reduce the potential for concurrency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * <li>Determining whether the locks are reentrant: can a thread with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * write lock reacquire it? Can it acquire a read lock while holding the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * write lock? Is the read lock itself reentrant?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * <li>Can the write lock be downgraded to a read lock without allowing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * an intervening writer? Can a read lock be upgraded to a write lock,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * in preference to other waiting readers or writers?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * You should consider all of these things when evaluating the suitability
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * of a given implementation for your application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * @see ReentrantReadWriteLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * @see Lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * @see ReentrantLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
public interface ReadWriteLock {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Returns the lock used for reading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 9242
diff changeset
   122
     * @return the lock used for reading
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    Lock readLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Returns the lock used for writing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 9242
diff changeset
   129
     * @return the lock used for writing
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    Lock writeLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
}