| ||||
|
Code - Class EDU.oswego.cs.dl.util.concurrent.SyncList1 /* 2 File: SyncList.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 1Aug1998 dl Create public version 12 */ 13 14 package EDU.oswego.cs.dl.util.concurrent; 15 import java.util.*; 16 17 /** 18 * SyncLists wrap Sync-based control around java.util.Lists. 19 * They support the following additional reader operations over 20 * SyncCollection: hashCode, equals, get, indexOf, lastIndexOf, 21 * subList. They support additional writer operations remove(int), 22 * set(int), add(int), addAll(int). The corresponding listIterators 23 * and are similarly extended. 24 * <p>[<a HREF="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>] 25 * @see SyncCollection 26 **/ 27 28 29 public class SyncList extends SyncCollection implements List { 30 31 /** 32 * Create a new SyncList protecting the given collection, 33 * and using the given sync to control both reader and writer methods. 34 * Common, reasonable choices for the sync argument include 35 * Mutex, ReentrantLock, and Semaphores initialized to 1. 36 **/ 37 public SyncList(List list, Sync sync) { 38 super (list, sync); 39 } 40 41 /** 42 * Create a new SyncList protecting the given list, 43 * and using the given ReadWriteLock to control reader and writer methods. 44 **/ 45 public SyncList(List list, ReadWriteLock rwl) { 46 super (list, rwl.readLock(), rwl.writeLock()); 47 } 48 49 /** 50 * Create a new SyncList protecting the given list, 51 * and using the given pair of locks to control reader and writer methods. 52 **/ 53 public SyncList(List list, Sync readLock, Sync writeLock) { 54 super(list, readLock, writeLock); 55 } 56 57 58 protected List baseList() { 59 return (List)c_; 60 } 61 62 63 public int hashCode() { 64 boolean wasInterrupted = beforeRead(); 65 try { 66 return c_.hashCode(); 67 } 68 finally { 69 afterRead(wasInterrupted); 70 } 71 } 72 73 public boolean equals(Object o) { 74 boolean wasInterrupted = beforeRead(); 75 try { 76 return c_.equals(o); 77 } 78 finally { 79 afterRead(wasInterrupted); 80 } 81 } 82 83 public Object get(int index) { 84 boolean wasInterrupted = beforeRead(); 85 try { 86 return baseList().get(index); 87 } 88 finally { 89 afterRead(wasInterrupted); 90 } 91 } 92 93 public int indexOf(Object o) { 94 boolean wasInterrupted = beforeRead(); 95 try { 96 return baseList().indexOf(o); 97 } 98 finally { 99 afterRead(wasInterrupted); 100 } 101 } 102 103 104 public int lastIndexOf(Object o) { 105 boolean wasInterrupted = beforeRead(); 106 try { 107 return baseList().lastIndexOf(o); 108 } 109 finally { 110 afterRead(wasInterrupted); 111 } 112 } 113 114 115 116 public List subList(int fromIndex, int toIndex) { 117 boolean wasInterrupted = beforeRead(); 118 try { 119 return new SyncList(baseList().subList(fromIndex, toIndex), rd_, wr_); 120 } 121 finally { 122 afterRead(wasInterrupted); 123 } 124 } 125 126 public Object set(int index, Object o) { 127 try { 128 wr_.acquire(); 129 try { 130 return baseList().set(index, o); 131 } 132 finally { 133 wr_.release(); 134 } 135 } 136 catch (InterruptedException ex) { 137 Thread.currentThread().interrupt(); 138 throw new UnsupportedOperationException(); 139 } 140 } 141 142 143 public Object remove(int index) { 144 try { 145 wr_.acquire(); 146 try { 147 return baseList().remove(index); 148 } 149 finally { 150 wr_.release(); 151 } 152 } 153 catch (InterruptedException ex) { 154 Thread.currentThread().interrupt(); 155 throw new UnsupportedOperationException(); 156 } 157 } 158 159 public void add(int index, Object o) { 160 try { 161 wr_.acquire(); 162 try { 163 baseList().add(index, o); 164 } 165 finally { 166 wr_.release(); 167 } 168 } 169 catch (InterruptedException ex) { 170 Thread.currentThread().interrupt(); 171 throw new UnsupportedOperationException(); 172 } 173 } 174 175 public boolean addAll(int index, Collection coll) { 176 try { 177 wr_.acquire(); 178 try { 179 return baseList().addAll(index, coll); 180 } 181 finally { 182 wr_.release(); 183 } 184 } 185 catch (InterruptedException ex) { 186 Thread.currentThread().interrupt(); 187 throw new UnsupportedOperationException(); 188 } 189 } 190 191 public ListIterator unprotectedListIterator() { 192 boolean wasInterrupted = beforeRead(); 193 try { 194 return baseList().listIterator(); 195 } 196 finally { 197 afterRead(wasInterrupted); 198 } 199 } 200 201 public ListIterator listIterator() { 202 boolean wasInterrupted = beforeRead(); 203 try { 204 return new SyncCollectionListIterator(baseList().listIterator()); 205 } 206 finally { 207 afterRead(wasInterrupted); 208 } 209 } 210 211 public ListIterator unprotectedListIterator(int index) { 212 boolean wasInterrupted = beforeRead(); 213 try { 214 return baseList().listIterator(index); 215 } 216 finally { 217 afterRead(wasInterrupted); 218 } 219 } 220 221 public ListIterator listIterator(int index) { 222 boolean wasInterrupted = beforeRead(); 223 try { 224 return new SyncCollectionListIterator(baseList().listIterator(index)); 225 } 226 finally { 227 afterRead(wasInterrupted); 228 } 229 } 230 231 232 public class SyncCollectionListIterator extends SyncCollectionIterator implements ListIterator { 233 234 SyncCollectionListIterator(Iterator baseIterator) { 235 super(baseIterator); 236 } 237 238 protected ListIterator baseListIterator() { 239 return (ListIterator)(baseIterator_); 240 } 241 242 public boolean hasPrevious() { 243 boolean wasInterrupted = beforeRead(); 244 try { 245 return baseListIterator().hasPrevious(); 246 } 247 finally { 248 afterRead(wasInterrupted); 249 } 250 } 251 252 public Object previous() { 253 boolean wasInterrupted = beforeRead(); 254 try { 255 return baseListIterator().previous(); 256 } 257 finally { 258 afterRead(wasInterrupted); 259 } 260 } 261 262 public int nextIndex() { 263 boolean wasInterrupted = beforeRead(); 264 try { 265 return baseListIterator().nextIndex(); 266 } 267 finally { 268 afterRead(wasInterrupted); 269 } 270 } 271 272 273 public int previousIndex() { 274 boolean wasInterrupted = beforeRead(); 275 try { 276 return baseListIterator().previousIndex(); 277 } 278 finally { 279 afterRead(wasInterrupted); 280 } 281 } 282 283 284 public void set(Object o) { 285 try { 286 wr_.acquire(); 287 try { 288 baseListIterator().set(o); 289 } 290 finally { 291 wr_.release(); 292 } 293 } 294 catch (InterruptedException ex) { 295 Thread.currentThread().interrupt(); 296 throw new UnsupportedOperationException(); 297 } 298 } 299 300 public void add(Object o) { 301 try { 302 wr_.acquire(); 303 try { 304 baseListIterator().add(o); 305 } 306 finally { 307 wr_.release(); 308 } 309 } 310 catch (InterruptedException ex) { 311 Thread.currentThread().interrupt(); 312 throw new UnsupportedOperationException(); 313 } 314 } 315 316 317 } 318 319 } 320 321 322 |
|||
Java API By Example, From Geeks To Geeks. |
Conditions of Use |
About Us
© 2002 - 2005, KickJava.com, or its affiliates
|