Print this page


Split Close
Expand all
Collapse all
          --- old/src/share/classes/java/util/concurrent/locks/ReentrantLock.java
          +++ new/src/share/classes/java/util/concurrent/locks/ReentrantLock.java
↓ open down ↓ 108 lines elided ↑ open up ↑
 109  109  public class ReentrantLock implements Lock, java.io.Serializable {
 110  110      private static final long serialVersionUID = 7373984872572414699L;
 111  111      /** Synchronizer providing all implementation mechanics */
 112  112      private final Sync sync;
 113  113  
 114  114      /**
 115  115       * Base of synchronization control for this lock. Subclassed
 116  116       * into fair and nonfair versions below. Uses AQS state to
 117  117       * represent the number of holds on the lock.
 118  118       */
 119      -    static abstract class Sync extends AbstractQueuedSynchronizer {
      119 +    abstract static class Sync extends AbstractQueuedSynchronizer {
 120  120          private static final long serialVersionUID = -5179523762034025860L;
 121  121  
 122  122          /**
 123  123           * Performs {@link Lock#lock}. The main reason for subclassing
 124  124           * is to allow fast path for nonfair version.
 125  125           */
 126  126          abstract void lock();
 127  127  
 128  128          /**
 129  129           * Performs non-fair tryLock.  tryAcquire is
↓ open down ↓ 63 lines elided ↑ open up ↑
 193  193          private void readObject(java.io.ObjectInputStream s)
 194  194              throws java.io.IOException, ClassNotFoundException {
 195  195              s.defaultReadObject();
 196  196              setState(0); // reset to unlocked state
 197  197          }
 198  198      }
 199  199  
 200  200      /**
 201  201       * Sync object for non-fair locks
 202  202       */
 203      -    final static class NonfairSync extends Sync {
      203 +    static final class NonfairSync extends Sync {
 204  204          private static final long serialVersionUID = 7316153563782823691L;
 205  205  
 206  206          /**
 207  207           * Performs lock.  Try immediate barge, backing up to normal
 208  208           * acquire on failure.
 209  209           */
 210  210          final void lock() {
 211  211              if (compareAndSetState(0, 1))
 212  212                  setExclusiveOwnerThread(Thread.currentThread());
 213  213              else
↓ open down ↓ 1 lines elided ↑ open up ↑
 215  215          }
 216  216  
 217  217          protected final boolean tryAcquire(int acquires) {
 218  218              return nonfairTryAcquire(acquires);
 219  219          }
 220  220      }
 221  221  
 222  222      /**
 223  223       * Sync object for fair locks
 224  224       */
 225      -    final static class FairSync extends Sync {
      225 +    static final class FairSync extends Sync {
 226  226          private static final long serialVersionUID = -3000897897090466540L;
 227  227  
 228  228          final void lock() {
 229  229              acquire(1);
 230  230          }
 231  231  
 232  232          /**
 233  233           * Fair version of tryAcquire.  Don't grant access unless
 234  234           * recursive call or no waiters or is first.
 235  235           */
↓ open down ↓ 26 lines elided ↑ open up ↑
 262  262          sync = new NonfairSync();
 263  263      }
 264  264  
 265  265      /**
 266  266       * Creates an instance of {@code ReentrantLock} with the
 267  267       * given fairness policy.
 268  268       *
 269  269       * @param fair {@code true} if this lock should use a fair ordering policy
 270  270       */
 271  271      public ReentrantLock(boolean fair) {
 272      -        sync = (fair)? new FairSync() : new NonfairSync();
      272 +        sync = fair ? new FairSync() : new NonfairSync();
 273  273      }
 274  274  
 275  275      /**
 276  276       * Acquires the lock.
 277  277       *
 278  278       * <p>Acquires the lock if it is not held by another thread and returns
 279  279       * immediately, setting the lock hold count to one.
 280  280       *
 281  281       * <p>If the current thread already holds the lock then the hold
 282  282       * count is incremented by one and the method returns immediately.
↓ open down ↓ 150 lines elided ↑ open up ↑
 433  433       * @param timeout the time to wait for the lock
 434  434       * @param unit the time unit of the timeout argument
 435  435       * @return {@code true} if the lock was free and was acquired by the
 436  436       *         current thread, or the lock was already held by the current
 437  437       *         thread; and {@code false} if the waiting time elapsed before
 438  438       *         the lock could be acquired
 439  439       * @throws InterruptedException if the current thread is interrupted
 440  440       * @throws NullPointerException if the time unit is null
 441  441       *
 442  442       */
 443      -    public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException {
      443 +    public boolean tryLock(long timeout, TimeUnit unit)
      444 +            throws InterruptedException {
 444  445          return sync.tryAcquireNanos(1, unit.toNanos(timeout));
 445  446      }
 446  447  
 447  448      /**
 448  449       * Attempts to release this lock.
 449  450       *
 450  451       * <p>If the current thread is the holder of this lock then the hold
 451  452       * count is decremented.  If the hold count is now zero then the lock
 452  453       * is released.  If the current thread is not the holder of this
 453  454       * lock then {@link IllegalMonitorStateException} is thrown.
↓ open down ↓ 316 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX