01: /*
02: File: ReadWriteLock.java
03:
04: Originally written by Doug Lea and released into the public domain.
05: This may be used for any purposes whatsoever without acknowledgment.
06: Thanks for the assistance and support of Sun Microsystems Labs,
07: and everyone contributing, testing, and using this code.
08:
09: History:
10: Date Who What
11: 11Jun1998 dl Create public version
12: */
13:
14: package EDU.oswego.cs.dl.util.concurrent;
15:
16: /**
17: * ReadWriteLocks maintain a pair of associated locks.
18: * The readLock may be held simultanously by multiple
19: * reader threads, so long as there are no writers. The writeLock
20: * is exclusive. ReadWrite locks are generally preferable to
21: * plain Sync locks or synchronized methods in cases where:
22: * <ul>
23: * <li> The methods in a class can be cleanly separated into
24: * those that only access (read) data vs those that
25: * modify (write).
26: * <li> Target applications generally have more readers than writers.
27: * <li> The methods are relatively time-consuming (as a rough
28: * rule of thumb, exceed more than a hundred instructions), so it
29: * pays to introduce a bit more overhead associated with
30: * ReadWrite locks compared to simple synchronized methods etc
31: * in order to allow concurrency among reader threads.
32: *
33: * </ul>
34: * Different implementation classes differ in policies surrounding
35: * which threads to prefer when there is
36: * contention. By far, the most commonly useful policy is
37: * WriterPreferenceReadWriteLock. The other implementations
38: * are targeted for less common, niche applications.
39: *<p>
40: * Standard usage:
41: * <pre>
42: * class X {
43: * ReadWriteLock rw;
44: * // ...
45: *
46: * public void read() throws InterruptedException {
47: * rw.readLock().acquire();
48: * try {
49: * // ... do the read
50: * }
51: * finally {
52: * rw.readlock().release()
53: * }
54: * }
55: *
56: *
57: * public void write() throws InterruptedException {
58: * rw.writeLock().acquire();
59: * try {
60: * // ... do the write
61: * }
62: * finally {
63: * rw.writelock().release()
64: * }
65: * }
66: * }
67: * </pre>
68: * @see Sync
69: * <p>[<a href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
70:
71: **/
72:
73: public interface ReadWriteLock {
74: /** get the readLock **/
75: Sync readLock();
76:
77: /** get the writeLock **/
78: Sync writeLock();
79: }
|