1. /*
  2. * @(#)ReadWriteLock.java 1.6 04/07/13
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.util.concurrent.locks;
  8. /**
  9. * A <tt>ReadWriteLock</tt> maintains a pair of associated {@link
  10. * Lock locks}, one for read-only operations and one for writing.
  11. * The {@link #readLock read lock} may be held simultaneously by
  12. * multiple reader threads, so long as there are no writers. The
  13. * {@link #writeLock write lock} is exclusive.
  14. *
  15. * <p>A read-write lock allows for a greater level of concurrency in
  16. * accessing shared data than that permitted by a mutual exclusion lock.
  17. * It exploits the fact that while only a single thread at a time (a
  18. * <em>writer</em> thread) can modify the shared data, in many cases any
  19. * number of threads can concurrently read the data (hence <em>reader</em>
  20. * threads).
  21. * In theory, the increase in concurrency permitted by the use of a read-write
  22. * lock will lead to performance improvements over the use of a mutual
  23. * exclusion lock. In practice this increase in concurrency will only be fully
  24. * realized on a multi-processor, and then only if the access patterns for
  25. * the shared data are suitable.
  26. *
  27. * <p>Whether or not a read-write lock will improve performance over the use
  28. * of a mutual exclusion lock depends on the frequency that the data is
  29. * read compared to being modified, the duration of the read and write
  30. * operations, and the contention for the data - that is, the number of
  31. * threads that will try to read or write the data at the same time.
  32. * For example, a collection that is initially populated with data and
  33. * thereafter infrequently modified, while being frequently searched
  34. * (such as a directory of some kind) is an ideal candidate for the use of
  35. * a read-write lock. However, if updates become frequent then the data
  36. * spends most of its time being exclusively locked and there is little, if any
  37. * increase in concurrency. Further, if the read operations are too short
  38. * the overhead of the read-write lock implementation (which is inherently
  39. * more complex than a mutual exclusion lock) can dominate the execution
  40. * cost, particularly as many read-write lock implementations still serialize
  41. * all threads through a small section of code. Ultimately, only profiling
  42. * and measurement will establish whether the use of a read-write lock is
  43. * suitable for your application.
  44. *
  45. *
  46. * <p>Although the basic operation of a read-write lock is straight-forward,
  47. * there are many policy decisions that an implementation must make, which
  48. * may affect the effectiveness of the read-write lock in a given application.
  49. * Examples of these policies include:
  50. * <ul>
  51. * <li>Determining whether to grant the read lock or the write lock, when
  52. * both readers and writers are waiting, at the time that a writer releases
  53. * the write lock. Writer preference is common, as writes are expected to be
  54. * short and infrequent. Reader preference is less common as it can lead to
  55. * lengthy delays for a write if the readers are frequent and long-lived as
  56. * expected. Fair, or "in-order" implementations are also possible.
  57. *
  58. * <li>Determining whether readers that request the read lock while a
  59. * reader is active and a writer is waiting, are granted the read lock.
  60. * Preference to the reader can delay the writer indefinitely, while
  61. * preference to the writer can reduce the potential for concurrency.
  62. *
  63. * <li>Determining whether the locks are reentrant: can a thread with the
  64. * write lock reacquire it? Can it acquire a read lock while holding the
  65. * write lock? Is the read lock itself reentrant?
  66. *
  67. * <li>Can the write lock be downgraded to a read lock without allowing
  68. * an intervening writer? Can a read lock be upgraded to a write lock,
  69. * in preference to other waiting readers or writers?
  70. *
  71. * </ul>
  72. * You should consider all of these things when evaluating the suitability
  73. * of a given implementation for your application.
  74. *
  75. * @see ReentrantReadWriteLock
  76. * @see Lock
  77. * @see ReentrantLock
  78. *
  79. * @since 1.5
  80. * @author Doug Lea
  81. */
  82. public interface ReadWriteLock {
  83. /**
  84. * Returns the lock used for reading.
  85. *
  86. * @return the lock used for reading.
  87. */
  88. Lock readLock();
  89. /**
  90. * Returns the lock used for writing.
  91. *
  92. * @return the lock used for writing.
  93. */
  94. Lock writeLock();
  95. }