KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > core > util > thread > ReadWriteLock


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.core.util.thread;
19
20 /*
21   File: ReadWriteLock.java
22
23   Originally written by Doug Lea and released into the public domain.
24   This may be used for any purposes whatsoever without acknowledgment.
25   Thanks for the assistance and support of Sun Microsystems Labs,
26   and everyone contributing, testing, and using this code.
27
28   History:
29   Date Who What
30   11Jun1998 dl Create public version
31 */

32
33
34
35 /**
36  * ReadWriteLocks maintain a pair of associated locks.
37  * The readLock may be held simultanously by multiple
38  * reader threads, so long as there are no writers. The writeLock
39  * is exclusive. ReadWrite locks are generally preferable to
40  * plain Sync locks or synchronized methods in cases where:
41  * <ul>
42  * <li> The methods in a class can be cleanly separated into
43  * those that only access (read) data vs those that
44  * modify (write).
45  * <li> Target applications generally have more readers than writers.
46  * <li> The methods are relatively time-consuming (as a rough
47  * rule of thumb, exceed more than a hundred instructions), so it
48  * pays to introduce a bit more overhead associated with
49  * ReadWrite locks compared to simple synchronized methods etc
50  * in order to allow concurrency among reader threads.
51  *
52  * </ul>
53  * Different implementation classes differ in policies surrounding
54  * which threads to prefer when there is
55  * contention. By far, the most commonly useful policy is
56  * WriterPreferenceReadWriteLock. The other implementations
57  * are targeted for less common, niche applications.
58  *<p>
59  * Standard usage:
60  * <pre>
61  * class X {
62  * ReadWriteLock rw;
63  * // ...
64  *
65  * public void read() throws InterruptedException {
66  * rw.readLock().acquire();
67  * try {
68  * // ... do the read
69  * }
70  * finally {
71  * rw.readlock().release()
72  * }
73  * }
74  *
75  *
76  * public void write() throws InterruptedException {
77  * rw.writeLock().acquire();
78  * try {
79  * // ... do the write
80  * }
81  * finally {
82  * rw.writelock().release()
83  * }
84  * }
85  * }
86  * </pre>
87  * <p>[<a HREF="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
88  *
89  * @since carbon 2.0
90  * @author Greg Hinkle, March 2003
91  * @version $Revision: 1.3 $($Author: dvoet $ / $Date: 2003/05/05 21:21:24 $)
92  **/

93 public interface ReadWriteLock {
94   /** get the readLock **/
95   Sync readLock();
96
97   /** get the writeLock **/
98   Sync writeLock();
99 }
100
101
Popular Tags