KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jofti > oswego > concurrent > ReadWriteLock


1 /*
2   File: ReadWriteLock.java
3
4   Originally written by Doug Lea and released into the public domain.
5   This may be used for any purposes whatsoever without acknowledgment.
6   Thanks for the assistance and support of Sun Microsystems Labs,
7   and everyone contributing, testing, and using this code.
8
9   History:
10   Date Who What
11   11Jun1998 dl Create public version
12 */

13
14
15 package com.jofti.oswego.concurrent;
16
17
18 /**
19  * ReadWriteLocks maintain a pair of associated locks.
20  * The readLock may be held simultanously by multiple
21  * reader threads, so long as there are no writers. The writeLock
22  * is exclusive. ReadWrite locks are generally preferable to
23  * plain Sync locks or synchronized methods in cases where:
24  * <ul>
25  * <li> The methods in a class can be cleanly separated into
26  * those that only access (read) data vs those that
27  * modify (write).
28  * <li> Target applications generally have more readers than writers.
29  * <li> The methods are relatively time-consuming (as a rough
30  * rule of thumb, exceed more than a hundred instructions), so it
31  * pays to introduce a bit more overhead associated with
32  * ReadWrite locks compared to simple synchronized methods etc
33  * in order to allow concurrency among reader threads.
34  *
35  * </ul>
36  * Different implementation classes differ in policies surrounding
37  * which threads to prefer when there is
38  * contention. By far, the most commonly useful policy is
39  * WriterPreferenceReadWriteLock. The other implementations
40  * are targeted for less common, niche applications.
41  *<p>
42  * Standard usage:
43  * <pre>
44  * class X {
45  * ReadWriteLock rw;
46  * // ...
47  *
48  * public void read() throws InterruptedException {
49  * rw.readLock().acquire();
50  * try {
51  * // ... do the read
52  * }
53  * finally {
54  * rw.readlock().release()
55  * }
56  * }
57  *
58  *
59  * public void write() throws InterruptedException {
60  * rw.writeLock().acquire();
61  * try {
62  * // ... do the write
63  * }
64  * finally {
65  * rw.writelock().release()
66  * }
67  * }
68  * }
69  * </pre>
70  * @see Sync
71  * <p>[<a HREF="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
72
73 **/

74
75 public interface ReadWriteLock {
76   /** get the readLock **/
77   Sync readLock();
78
79   /** get the writeLock **/
80   Sync writeLock();
81 }
82
83
Popular Tags