--- jsr166/src/jsr166e/SequenceLock.java 2011/08/01 06:19:47 1.11 +++ jsr166/src/jsr166e/SequenceLock.java 2011/12/19 19:18:35 1.17 @@ -35,7 +35,7 @@ import java.io.IOException; *
Except for the lack of support for specified fairness policies, * or {@link Condition} objects, a SequenceLock can be used in the * same way as {@link ReentrantLock}. It provides similar status and - * monitoring methods such as {@link #isHeldByCurrentThread}. + * monitoring methods, such as {@link #isHeldByCurrentThread}. * SequenceLocks may be preferable in contexts in which multiple * threads invoke short read-only methods much more frequently than * fully locked methods. @@ -53,51 +53,54 @@ import java.io.IOException; * switching to locking mode. While conceptually straightforward, * expressing these ideas can be verbose. For example: * - *
{@code + ** * @since 1.8 * @author Doug Lea @@ -106,6 +109,8 @@ public class SequenceLock implements Loc private static final long serialVersionUID = 7373984872572414699L; static final class Sync extends AbstractQueuedLongSynchronizer { + static final long serialVersionUID = 2540673546047039555L; + /** * The number of times to spin in lock() and awaitAvailability(). */ @@ -157,7 +162,7 @@ public class SequenceLock implements Loc public final long tryAcquireShared(long unused) { return (((getState() & 1L) == 0L) ? 1L : - (getExclusiveOwnerThread() == Thread.currentThread()) ? 0L: + (getExclusiveOwnerThread() == Thread.currentThread()) ? 0L: -1L); } @@ -303,10 +308,10 @@ public class SequenceLock implements Loc * @param timeout the time to wait for availability * @param unit the time unit of the timeout argument * @return the current sequence number if the lock is available - * upon return from this method. + * upon return from this method * @throws InterruptedException if the current thread is interrupted * @throws TimeoutException if the lock was not available within - * the specified waiting time. + * the specified waiting time * @throws NullPointerException if the time unit is null */ public long tryAwaitAvailability(long timeout, TimeUnit unit) @@ -498,7 +503,7 @@ public class SequenceLock implements Loc * @throws IllegalMonitorStateException if the current thread does not * hold this lock */ - public void unlock() { sync.release(1); } + public void unlock() { sync.release(1); } /** * Throws UnsupportedOperationException. SequenceLocks @@ -506,7 +511,7 @@ public class SequenceLock implements Loc * * @throws UnsupportedOperationException */ - public Condition newCondition() { + public Condition newCondition() { throw new UnsupportedOperationException(); } @@ -522,7 +527,7 @@ public class SequenceLock implements Loc * @return the number of holds on this lock by the current thread, * or zero if this lock is not held by the current thread */ - public long getHoldCount() { return sync.getHoldCount(); } + public long getHoldCount() { return sync.getHoldCount(); } /** * Queries if this lock is held by the current thread.{@code * class Point { - * private volatile double x, y; - * private final SequenceLock sl = new SequenceLock(); + * private volatile double x, y; + * private final SequenceLock sl = new SequenceLock(); * - * void move(double deltaX, double deltaY) { // an exclusively locked method - * sl.lock(); - * try { - * x += deltaX; - * y += deltaY; - * } finally { - * sl.unlock(); - * } - * } + * // an exclusively locked method + * void move(double deltaX, double deltaY) { + * sl.lock(); + * try { + * x += deltaX; + * y += deltaY; + * } finally { + * sl.unlock(); + * } + * } * - * double distanceFromOriginV1() { // A read-only method - * double currentX, currentY; - * long seq; - * do { - * seq = sl.awaitAvailability(); - * currentX = x; - * currentY = y; - * } while (sl.getSequence() != seq); // retry if sequence changed - * return Math.sqrt(currentX * currentX + currentY * currentY); - * } + * // A read-only method + * double distanceFromOriginV1() { + * double currentX, currentY; + * long seq; + * do { + * seq = sl.awaitAvailability(); + * currentX = x; + * currentY = y; + * } while (sl.getSequence() != seq); // retry if sequence changed + * return Math.sqrt(currentX * currentX + currentY * currentY); + * } * - * double distanceFromOriginV2() { // Uses bounded retries before locking - * double currentX, currentY; - * long seq; - * int retries = RETRIES_BEFORE_LOCKING; // for example 8 - * try { - * do { - * if (--retries < 0) - * sl.lock(); - * seq = sl.awaitAvailability(); - * currentX = x; - * currentY = y; - * } while (sl.getSequence() != seq); - * } finally { - * if (retries < 0) - * sl.unlock(); - * } - * return Math.sqrt(currentX * currentX + currentY * currentY); - * } - *}}+ * // Uses bounded retries before locking + * double distanceFromOriginV2() { + * double currentX, currentY; + * long seq; + * int retries = RETRIES_BEFORE_LOCKING; // for example 8 + * try { + * do { + * if (--retries < 0) + * sl.lock(); + * seq = sl.awaitAvailability(); + * currentX = x; + * currentY = y; + * } while (sl.getSequence() != seq); + * } finally { + * if (retries < 0) + * sl.unlock(); + * } + * return Math.sqrt(currentX * currentX + currentY * currentY); + * } + * }}