1. /*
  2. * @(#)Lock.java 1.4 03/12/19
  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. import java.util.concurrent.TimeUnit;
  9. /**
  10. * <tt>Lock</tt> implementations provide more extensive locking
  11. * operations than can be obtained using <tt>synchronized</tt> methods
  12. * and statements. They allow more flexible structuring, may have
  13. * quite different properties, and may support multiple associated
  14. * {@link Condition} objects.
  15. *
  16. * <p>A lock is a tool for controlling access to a shared resource by
  17. * multiple threads. Commonly, a lock provides exclusive access to a
  18. * shared resource: only one thread at a time can acquire the lock and
  19. * all access to the shared resource requires that the lock be
  20. * acquired first. However, some locks may allow concurrent access to
  21. * a shared resource, such as the read lock of a {@link
  22. * ReadWriteLock}.
  23. *
  24. * <p>The use of <tt>synchronized</tt> methods or statements provides
  25. * access to the implicit monitor lock associated with every object, but
  26. * forces all lock acquisition and release to occur in a block-structured way:
  27. * when multiple locks are acquired they must be released in the opposite
  28. * order, and all locks must be released in the same lexical scope in which
  29. * they were acquired.
  30. *
  31. * <p>While the scoping mechanism for <tt>synchronized</tt> methods
  32. * and statements makes it much easier to program with monitor locks,
  33. * and helps avoid many common programming errors involving locks,
  34. * there are occasions where you need to work with locks in a more
  35. * flexible way. For example, some algorithms for traversing
  36. * concurrently accessed data structures require the use of
  37. * "hand-over-hand" or "chain locking": you
  38. * acquire the lock of node A, then node B, then release A and acquire
  39. * C, then release B and acquire D and so on. Implementations of the
  40. * <tt>Lock</tt> interface enable the use of such techniques by
  41. * allowing a lock to be acquired and released in different scopes,
  42. * and allowing multiple locks to be acquired and released in any
  43. * order.
  44. *
  45. * <p>With this increased flexibility comes additional
  46. * responsibility. The absence of block-structured locking removes the
  47. * automatic release of locks that occurs with <tt>synchronized</tt>
  48. * methods and statements. In most cases, the following idiom
  49. * should be used:
  50. *
  51. * <pre><tt> Lock l = ...;
  52. * l.lock();
  53. * try {
  54. * // access the resource protected by this lock
  55. * } finally {
  56. * l.unlock();
  57. * }
  58. * </tt></pre>
  59. *
  60. * When locking and unlocking occur in different scopes, care must be
  61. * taken to ensure that all code that is executed while the lock is
  62. * held is protected by try-finally or try-catch to ensure that the
  63. * lock is released when necessary.
  64. *
  65. * <p><tt>Lock</tt> implementations provide additional functionality
  66. * over the use of <tt>synchronized</tt> methods and statements by
  67. * providing a non-blocking attempt to acquire a lock ({@link
  68. * #tryLock()}), an attempt to acquire the lock that can be
  69. * interrupted ({@link #lockInterruptibly}, and an attempt to acquire
  70. * the lock that can timeout ({@link #tryLock(long, TimeUnit)}).
  71. *
  72. * <p>A <tt>Lock</tt> class can also provide behavior and semantics
  73. * that is quite different from that of the implicit monitor lock,
  74. * such as guaranteed ordering, non-reentrant usage, or deadlock
  75. * detection. If an implementation provides such specialized semantics
  76. * then the implementation must document those semantics.
  77. *
  78. * <p>Note that <tt>Lock</tt> instances are just normal objects and can
  79. * themselves be used as the target in a <tt>synchronized</tt> statement.
  80. * Acquiring the
  81. * monitor lock of a <tt>Lock</tt> instance has no specified relationship
  82. * with invoking any of the {@link #lock} methods of that instance.
  83. * It is recommended that to avoid confusion you never use <tt>Lock</tt>
  84. * instances in this way, except within their own implementation.
  85. *
  86. * <p>Except where noted, passing a <tt>null</tt> value for any
  87. * parameter will result in a {@link NullPointerException} being
  88. * thrown.
  89. *
  90. * <h3>Memory Synchronization</h3>
  91. * <p>All <tt>Lock</tt> implementations <em>must</em> enforce the same
  92. * memory synchronization semantics as provided by the built-in monitor lock:
  93. * <ul>
  94. * <li>A successful lock operation acts like a successful
  95. * <tt>monitorEnter</tt> action
  96. * <li>A successful <tt>unlock</tt> operation acts like a successful
  97. * <tt>monitorExit</tt> action
  98. * </ul>
  99. *
  100. * Unsuccessful locking and unlocking operations, and reentrant
  101. * locking/unlocking operations, do not require any memory
  102. * synchronization effects.
  103. *
  104. * <h3>Implementation Considerations</h3>
  105. *
  106. * <p> The three forms of lock acquisition (interruptible,
  107. * non-interruptible, and timed) may differ in their performance
  108. * characteristics, ordering guarantees, or other implementation
  109. * qualities. Further, the ability to interrupt the <em>ongoing</em>
  110. * acquisition of a lock may not be available in a given <tt>Lock</tt>
  111. * class. Consequently, an implementation is not required to define
  112. * exactly the same guarantees or semantics for all three forms of
  113. * lock acquisition, nor is it required to support interruption of an
  114. * ongoing lock acquisition. An implementation is required to clearly
  115. * document the semantics and guarantees provided by each of the
  116. * locking methods. It must also obey the interruption semantics as
  117. * defined in this interface, to the extent that interruption of lock
  118. * acquisition is supported: which is either totally, or only on
  119. * method entry.
  120. *
  121. * <p>As interruption generally implies cancellation, and checks for
  122. * interruption are often infrequent, an implementation can favor responding
  123. * to an interrupt over normal method return. This is true even if it can be
  124. * shown that the interrupt occurred after another action may have unblocked
  125. * the thread. An implementation should document this behavior.
  126. *
  127. *
  128. * @see ReentrantLock
  129. * @see Condition
  130. * @see ReadWriteLock
  131. *
  132. * @since 1.5
  133. * @author Doug Lea
  134. *
  135. **/
  136. public interface Lock {
  137. /**
  138. * Acquires the lock.
  139. * <p>If the lock is not available then
  140. * the current thread becomes disabled for thread scheduling
  141. * purposes and lies dormant until the lock has been acquired.
  142. * <p><b>Implementation Considerations</b>
  143. * <p>A <tt>Lock</tt> implementation may be able to detect
  144. * erroneous use of the lock, such as an invocation that would cause
  145. * deadlock, and may throw an (unchecked) exception in such circumstances.
  146. * The circumstances and the exception type must be documented by that
  147. * <tt>Lock</tt> implementation.
  148. *
  149. **/
  150. void lock();
  151. /**
  152. * Acquires the lock unless the current thread is
  153. * {@link Thread#interrupt interrupted}.
  154. * <p>Acquires the lock if it is available and returns immediately.
  155. * <p>If the lock is not available then
  156. * the current thread becomes disabled for thread scheduling
  157. * purposes and lies dormant until one of two things happens:
  158. * <ul>
  159. * <li>The lock is acquired by the current thread; or
  160. * <li>Some other thread {@link Thread#interrupt interrupts} the current
  161. * thread, and interruption of lock acquisition is supported.
  162. * </ul>
  163. * <p>If the current thread:
  164. * <ul>
  165. * <li>has its interrupted status set on entry to this method; or
  166. * <li>is {@link Thread#interrupt interrupted} while acquiring
  167. * the lock, and interruption of lock acquisition is supported,
  168. * </ul>
  169. * then {@link InterruptedException} is thrown and the current thread's
  170. * interrupted status is cleared.
  171. *
  172. * <p><b>Implementation Considerations</b>
  173. *
  174. * <p>The ability to interrupt a lock acquisition in some
  175. * implementations may not be possible, and if possible may be an
  176. * expensive operation. The programmer should be aware that this
  177. * may be the case. An implementation should document when this is
  178. * the case.
  179. *
  180. * <p>An implementation can favor responding to an interrupt over
  181. * normal method return.
  182. *
  183. * <p>A <tt>Lock</tt> implementation may be able to detect
  184. * erroneous use of the lock, such as an invocation that would
  185. * cause deadlock, and may throw an (unchecked) exception in such
  186. * circumstances. The circumstances and the exception type must
  187. * be documented by that <tt>Lock</tt> implementation.
  188. *
  189. * @throws InterruptedException if the current thread is interrupted
  190. * while acquiring the lock (and interruption of lock acquisition is
  191. * supported).
  192. *
  193. * @see Thread#interrupt
  194. *
  195. **/
  196. void lockInterruptibly() throws InterruptedException;
  197. /**
  198. * Acquires the lock only if it is free at the time of invocation.
  199. * <p>Acquires the lock if it is available and returns immediately
  200. * with the value <tt>true</tt>.
  201. * If the lock is not available then this method will return
  202. * immediately with the value <tt>false</tt>.
  203. * <p>A typical usage idiom for this method would be:
  204. * <pre>
  205. * Lock lock = ...;
  206. * if (lock.tryLock()) {
  207. * try {
  208. * // manipulate protected state
  209. * } finally {
  210. * lock.unlock();
  211. * }
  212. * } else {
  213. * // perform alternative actions
  214. * }
  215. * </pre>
  216. * This usage ensures that the lock is unlocked if it was acquired, and
  217. * doesn't try to unlock if the lock was not acquired.
  218. *
  219. * @return <tt>true</tt> if the lock was acquired and <tt>false</tt>
  220. * otherwise.
  221. **/
  222. boolean tryLock();
  223. /**
  224. * Acquires the lock if it is free within the given waiting time and the
  225. * current thread has not been {@link Thread#interrupt interrupted}.
  226. *
  227. * <p>If the lock is available this method returns immediately
  228. * with the value <tt>true</tt>.
  229. * If the lock is not available then
  230. * the current thread becomes disabled for thread scheduling
  231. * purposes and lies dormant until one of three things happens:
  232. * <ul>
  233. * <li>The lock is acquired by the current thread; or
  234. * <li>Some other thread {@link Thread#interrupt interrupts} the current
  235. * thread, and interruption of lock acquisition is supported; or
  236. * <li>The specified waiting time elapses
  237. * </ul>
  238. * <p>If the lock is acquired then the value <tt>true</tt> is returned.
  239. * <p>If the current thread:
  240. * <ul>
  241. * <li>has its interrupted status set on entry to this method; or
  242. * <li>is {@link Thread#interrupt interrupted} while acquiring
  243. * the lock, and interruption of lock acquisition is supported,
  244. * </ul>
  245. * then {@link InterruptedException} is thrown and the current thread's
  246. * interrupted status is cleared.
  247. * <p>If the specified waiting time elapses then the value <tt>false</tt>
  248. * is returned.
  249. * If the time is
  250. * less than or equal to zero, the method will not wait at all.
  251. *
  252. * <p><b>Implementation Considerations</b>
  253. * <p>The ability to interrupt a lock acquisition in some implementations
  254. * may not be possible, and if possible may
  255. * be an expensive operation.
  256. * The programmer should be aware that this may be the case. An
  257. * implementation should document when this is the case.
  258. * <p>An implementation can favor responding to an interrupt over normal
  259. * method return, or reporting a timeout.
  260. * <p>A <tt>Lock</tt> implementation may be able to detect
  261. * erroneous use of the lock, such as an invocation that would cause
  262. * deadlock, and may throw an (unchecked) exception in such circumstances.
  263. * The circumstances and the exception type must be documented by that
  264. * <tt>Lock</tt> implementation.
  265. *
  266. * @param time the maximum time to wait for the lock
  267. * @param unit the time unit of the <tt>time</tt> argument.
  268. * @return <tt>true</tt> if the lock was acquired and <tt>false</tt>
  269. * if the waiting time elapsed before the lock was acquired.
  270. *
  271. * @throws InterruptedException if the current thread is interrupted
  272. * while acquiring the lock (and interruption of lock acquisition is
  273. * supported).
  274. *
  275. * @see Thread#interrupt
  276. *
  277. **/
  278. boolean tryLock(long time, TimeUnit unit) throws InterruptedException;
  279. /**
  280. * Releases the lock.
  281. * <p><b>Implementation Considerations</b>
  282. * <p>A <tt>Lock</tt> implementation will usually impose
  283. * restrictions on which thread can release a lock (typically only the
  284. * holder of the lock can release it) and may throw
  285. * an (unchecked) exception if the restriction is violated.
  286. * Any restrictions and the exception
  287. * type must be documented by that <tt>Lock</tt> implementation.
  288. **/
  289. void unlock();
  290. /**
  291. * Returns a new {@link Condition} instance that is bound to this
  292. * <tt>Lock</tt> instance.
  293. * <p>Before waiting on the condition the lock must be held by the
  294. * current thread.
  295. * A call to {@link Condition#await()} will atomically release the lock
  296. * before waiting and re-acquire the lock before the wait returns.
  297. * <p><b>Implementation Considerations</b>
  298. * <p>The exact operation of the {@link Condition} instance depends on the
  299. * <tt>Lock</tt> implementation and must be documented by that
  300. * implementation.
  301. *
  302. * @return A new {@link Condition} instance for this <tt>Lock</tt>
  303. * instance.
  304. * @throws UnsupportedOperationException if this <tt>Lock</tt>
  305. * implementation does not support conditions.
  306. **/
  307. Condition newCondition();
  308. }