1 13 14 package EDU.oswego.cs.dl.util.concurrent; 15 import java.util.*; 16 17 186 187 188 public class SyncCollection implements Collection { 189 protected final Collection c_; protected final Sync rd_; protected final Sync wr_; 193 protected final SynchronizedLong syncFailures_ = new SynchronizedLong(0); 194 195 206 public SyncCollection(Collection collection, Sync sync) { 207 this (collection, sync, sync); 208 } 209 210 211 221 public SyncCollection(Collection collection, ReadWriteLock rwl) { 222 this (collection, rwl.readLock(), rwl.writeLock()); 223 } 224 225 229 public SyncCollection(Collection collection, Sync readLock, Sync writeLock) { 230 c_ = collection; 231 rd_ = readLock; 232 wr_ = writeLock; 233 } 234 235 238 239 public Sync readerSync() { 240 return rd_; 241 } 242 243 246 247 public Sync writerSync() { 248 return wr_; 249 } 250 251 254 public long syncFailures() { 255 return syncFailures_.get(); 256 } 257 258 259 260 protected boolean beforeRead() { 261 try { 262 rd_.acquire(); 263 return false; 264 } 265 catch (InterruptedException ex) { 266 syncFailures_.increment(); 267 return true; 268 } 269 } 270 271 272 protected void afterRead(boolean wasInterrupted) { 273 if (wasInterrupted) { 274 Thread.currentThread().interrupt(); 275 } 276 else 277 rd_.release(); 278 } 279 280 281 282 public int size() { 283 boolean wasInterrupted = beforeRead(); 284 try { 285 return c_.size(); 286 } 287 finally { 288 afterRead(wasInterrupted); 289 } 290 } 291 292 public boolean isEmpty() { 293 boolean wasInterrupted = beforeRead(); 294 try { 295 return c_.isEmpty(); 296 } 297 finally { 298 afterRead(wasInterrupted); 299 } 300 } 301 302 public boolean contains(Object o) { 303 boolean wasInterrupted = beforeRead(); 304 try { 305 return c_.contains(o); 306 } 307 finally { 308 afterRead(wasInterrupted); 309 } 310 } 311 312 public Object [] toArray() { 313 boolean wasInterrupted = beforeRead(); 314 try { 315 return c_.toArray(); 316 } 317 finally { 318 afterRead(wasInterrupted); 319 } 320 } 321 322 public Object [] toArray(Object [] a) { 323 boolean wasInterrupted = beforeRead(); 324 try { 325 return c_.toArray(a); 326 } 327 finally { 328 afterRead(wasInterrupted); 329 } 330 } 331 332 public boolean containsAll(Collection coll) { 333 boolean wasInterrupted = beforeRead(); 334 try { 335 return c_.containsAll(coll); 336 } 337 finally { 338 afterRead(wasInterrupted); 339 } 340 } 341 342 343 public boolean add(Object o) { 344 try { 345 wr_.acquire(); 346 try { 347 return c_.add(o); 348 } 349 finally { 350 wr_.release(); 351 } 352 } 353 catch (InterruptedException ex) { 354 Thread.currentThread().interrupt(); 355 throw new UnsupportedOperationException (); 356 } 357 } 358 359 public boolean remove(Object o) { 360 try { 361 wr_.acquire(); 362 try { 363 return c_.remove(o); 364 } 365 finally { 366 wr_.release(); 367 } 368 } 369 catch (InterruptedException ex) { 370 Thread.currentThread().interrupt(); 371 throw new UnsupportedOperationException (); 372 } 373 } 374 375 public boolean addAll(Collection coll) { 376 try { 377 wr_.acquire(); 378 try { 379 return c_.addAll(coll); 380 } 381 finally { 382 wr_.release(); 383 } 384 } 385 catch (InterruptedException ex) { 386 Thread.currentThread().interrupt(); 387 throw new UnsupportedOperationException (); 388 } 389 } 390 391 public boolean removeAll(Collection coll) { 392 try { 393 wr_.acquire(); 394 try { 395 return c_.removeAll(coll); 396 } 397 finally { 398 wr_.release(); 399 } 400 } 401 catch (InterruptedException ex) { 402 Thread.currentThread().interrupt(); 403 throw new UnsupportedOperationException (); 404 } 405 } 406 407 408 public boolean retainAll(Collection coll) { 409 try { 410 wr_.acquire(); 411 try { 412 return c_.retainAll(coll); 413 } 414 finally { 415 wr_.release(); 416 } 417 } 418 catch (InterruptedException ex) { 419 Thread.currentThread().interrupt(); 420 throw new UnsupportedOperationException (); 421 } 422 } 423 424 425 public void clear() { 426 try { 427 wr_.acquire(); 428 try { 429 c_.clear(); 430 } 431 finally { 432 wr_.release(); 433 } 434 } 435 catch (InterruptedException ex) { 436 Thread.currentThread().interrupt(); 437 throw new UnsupportedOperationException (); 438 } 439 } 440 441 442 443 public Iterator unprotectedIterator() { 444 boolean wasInterrupted = beforeRead(); 445 try { 446 return c_.iterator(); 447 } 448 finally { 449 afterRead(wasInterrupted); 450 } 451 } 452 453 public Iterator iterator() { 454 boolean wasInterrupted = beforeRead(); 455 try { 456 return new SyncCollectionIterator(c_.iterator()); 457 } 458 finally { 459 afterRead(wasInterrupted); 460 } 461 } 462 463 public class SyncCollectionIterator implements Iterator { 464 protected final Iterator baseIterator_; 465 466 SyncCollectionIterator(Iterator baseIterator) { 467 baseIterator_ = baseIterator; 468 } 469 470 public boolean hasNext() { 471 boolean wasInterrupted = beforeRead(); 472 try { 473 return baseIterator_.hasNext(); 474 } 475 finally { 476 afterRead(wasInterrupted); 477 } 478 } 479 480 public Object next() { 481 boolean wasInterrupted = beforeRead(); 482 try { 483 return baseIterator_.next(); 484 } 485 finally { 486 afterRead(wasInterrupted); 487 } 488 } 489 490 public void remove() { 491 try { 492 wr_.acquire(); 493 try { 494 baseIterator_.remove(); 495 } 496 finally { 497 wr_.release(); 498 } 499 } 500 catch (InterruptedException ex) { 501 Thread.currentThread().interrupt(); 502 throw new UnsupportedOperationException (); 503 } 504 } 505 506 } 507 } 508 509 510 | Popular Tags |