Print this page


Split Close
Expand all
Collapse all
          --- old/src/share/classes/java/util/concurrent/locks/AbstractQueuedSynchronizer.java
          +++ new/src/share/classes/java/util/concurrent/locks/AbstractQueuedSynchronizer.java
↓ open down ↓ 257 lines elided ↑ open up ↑
 258  258   * fire. Because a latch is non-exclusive, it uses the <tt>shared</tt>
 259  259   * acquire and release methods.
 260  260   *
 261  261   * <pre>
 262  262   * class BooleanLatch {
 263  263   *
 264  264   *   private static class Sync extends AbstractQueuedSynchronizer {
 265  265   *     boolean isSignalled() { return getState() != 0; }
 266  266   *
 267  267   *     protected int tryAcquireShared(int ignore) {
 268      - *       return isSignalled()? 1 : -1;
      268 + *       return isSignalled() ? 1 : -1;
 269  269   *     }
 270  270   *
 271  271   *     protected boolean tryReleaseShared(int ignore) {
 272  272   *       setState(1);
 273  273   *       return true;
 274  274   *     }
 275  275   *   }
 276  276   *
 277  277   *   private final Sync sync = new Sync();
 278  278   *   public boolean isSignalled() { return sync.isSignalled(); }
↓ open down ↓ 927 lines elided ↑ open up ↑
1206 1206       * success.  Otherwise the thread is queued, possibly repeatedly
1207 1207       * blocking and unblocking, invoking {@link #tryAcquire}
1208 1208       * until success or the thread is interrupted.  This method can be
1209 1209       * used to implement method {@link Lock#lockInterruptibly}.
1210 1210       *
1211 1211       * @param arg the acquire argument.  This value is conveyed to
1212 1212       *        {@link #tryAcquire} but is otherwise uninterpreted and
1213 1213       *        can represent anything you like.
1214 1214       * @throws InterruptedException if the current thread is interrupted
1215 1215       */
1216      -    public final void acquireInterruptibly(int arg) throws InterruptedException {
     1216 +    public final void acquireInterruptibly(int arg)
     1217 +            throws InterruptedException {
1217 1218          if (Thread.interrupted())
1218 1219              throw new InterruptedException();
1219 1220          if (!tryAcquire(arg))
1220 1221              doAcquireInterruptibly(arg);
1221 1222      }
1222 1223  
1223 1224      /**
1224 1225       * Attempts to acquire in exclusive mode, aborting if interrupted,
1225 1226       * and failing if the given timeout elapses.  Implemented by first
1226 1227       * checking interrupt status, then invoking at least once {@link
↓ open down ↓ 3 lines elided ↑ open up ↑
1230 1231       * or the timeout elapses.  This method can be used to implement
1231 1232       * method {@link Lock#tryLock(long, TimeUnit)}.
1232 1233       *
1233 1234       * @param arg the acquire argument.  This value is conveyed to
1234 1235       *        {@link #tryAcquire} but is otherwise uninterpreted and
1235 1236       *        can represent anything you like.
1236 1237       * @param nanosTimeout the maximum number of nanoseconds to wait
1237 1238       * @return {@code true} if acquired; {@code false} if timed out
1238 1239       * @throws InterruptedException if the current thread is interrupted
1239 1240       */
1240      -    public final boolean tryAcquireNanos(int arg, long nanosTimeout) throws InterruptedException {
     1241 +    public final boolean tryAcquireNanos(int arg, long nanosTimeout)
     1242 +            throws InterruptedException {
1241 1243          if (Thread.interrupted())
1242 1244              throw new InterruptedException();
1243 1245          return tryAcquire(arg) ||
1244 1246              doAcquireNanos(arg, nanosTimeout);
1245 1247      }
1246 1248  
1247 1249      /**
1248 1250       * Releases in exclusive mode.  Implemented by unblocking one or
1249 1251       * more threads if {@link #tryRelease} returns true.
1250 1252       * This method can be used to implement method {@link Lock#unlock}.
↓ open down ↓ 35 lines elided ↑ open up ↑
1286 1288       * {@link #tryAcquireShared}, returning on success.  Otherwise the
1287 1289       * thread is queued, possibly repeatedly blocking and unblocking,
1288 1290       * invoking {@link #tryAcquireShared} until success or the thread
1289 1291       * is interrupted.
1290 1292       * @param arg the acquire argument
1291 1293       * This value is conveyed to {@link #tryAcquireShared} but is
1292 1294       * otherwise uninterpreted and can represent anything
1293 1295       * you like.
1294 1296       * @throws InterruptedException if the current thread is interrupted
1295 1297       */
1296      -    public final void acquireSharedInterruptibly(int arg) throws InterruptedException {
     1298 +    public final void acquireSharedInterruptibly(int arg)
     1299 +            throws InterruptedException {
1297 1300          if (Thread.interrupted())
1298 1301              throw new InterruptedException();
1299 1302          if (tryAcquireShared(arg) < 0)
1300 1303              doAcquireSharedInterruptibly(arg);
1301 1304      }
1302 1305  
1303 1306      /**
1304 1307       * Attempts to acquire in shared mode, aborting if interrupted, and
1305 1308       * failing if the given timeout elapses.  Implemented by first
1306 1309       * checking interrupt status, then invoking at least once {@link
↓ open down ↓ 2 lines elided ↑ open up ↑
1309 1312       * invoking {@link #tryAcquireShared} until success or the thread
1310 1313       * is interrupted or the timeout elapses.
1311 1314       *
1312 1315       * @param arg the acquire argument.  This value is conveyed to
1313 1316       *        {@link #tryAcquireShared} but is otherwise uninterpreted
1314 1317       *        and can represent anything you like.
1315 1318       * @param nanosTimeout the maximum number of nanoseconds to wait
1316 1319       * @return {@code true} if acquired; {@code false} if timed out
1317 1320       * @throws InterruptedException if the current thread is interrupted
1318 1321       */
1319      -    public final boolean tryAcquireSharedNanos(int arg, long nanosTimeout) throws InterruptedException {
     1322 +    public final boolean tryAcquireSharedNanos(int arg, long nanosTimeout)
     1323 +            throws InterruptedException {
1320 1324          if (Thread.interrupted())
1321 1325              throw new InterruptedException();
1322 1326          return tryAcquireShared(arg) >= 0 ||
1323 1327              doAcquireSharedNanos(arg, nanosTimeout);
1324 1328      }
1325 1329  
1326 1330      /**
1327 1331       * Releases in shared mode.  Implemented by unblocking one or more
1328 1332       * threads if {@link #tryReleaseShared} returns true.
1329 1333       *
↓ open down ↓ 725 lines elided ↑ open up ↑
2055 2059           * <li> Save lock state returned by {@link #getState}.
2056 2060           * <li> Invoke {@link #release} with
2057 2061           *      saved state as argument, throwing
2058 2062           *      IllegalMonitorStateException if it fails.
2059 2063           * <li> Block until signalled, interrupted, or timed out.
2060 2064           * <li> Reacquire by invoking specialized version of
2061 2065           *      {@link #acquire} with saved state as argument.
2062 2066           * <li> If interrupted while blocked in step 4, throw InterruptedException.
2063 2067           * </ol>
2064 2068           */
2065      -        public final long awaitNanos(long nanosTimeout) throws InterruptedException {
     2069 +        public final long awaitNanos(long nanosTimeout)
     2070 +                throws InterruptedException {
2066 2071              if (Thread.interrupted())
2067 2072                  throw new InterruptedException();
2068 2073              Node node = addConditionWaiter();
2069 2074              int savedState = fullyRelease(node);
2070 2075              long lastTime = System.nanoTime();
2071 2076              int interruptMode = 0;
2072 2077              while (!isOnSyncQueue(node)) {
2073 2078                  if (nanosTimeout <= 0L) {
2074 2079                      transferAfterCancelledWait(node);
2075 2080                      break;
↓ open down ↓ 23 lines elided ↑ open up ↑
2099 2104           * <li> Invoke {@link #release} with
2100 2105           *      saved state as argument, throwing
2101 2106           *      IllegalMonitorStateException if it fails.
2102 2107           * <li> Block until signalled, interrupted, or timed out.
2103 2108           * <li> Reacquire by invoking specialized version of
2104 2109           *      {@link #acquire} with saved state as argument.
2105 2110           * <li> If interrupted while blocked in step 4, throw InterruptedException.
2106 2111           * <li> If timed out while blocked in step 4, return false, else true.
2107 2112           * </ol>
2108 2113           */
2109      -        public final boolean awaitUntil(Date deadline) throws InterruptedException {
     2114 +        public final boolean awaitUntil(Date deadline)
     2115 +                throws InterruptedException {
2110 2116              if (deadline == null)
2111 2117                  throw new NullPointerException();
2112 2118              long abstime = deadline.getTime();
2113 2119              if (Thread.interrupted())
2114 2120                  throw new InterruptedException();
2115 2121              Node node = addConditionWaiter();
2116 2122              int savedState = fullyRelease(node);
2117 2123              boolean timedout = false;
2118 2124              int interruptMode = 0;
2119 2125              while (!isOnSyncQueue(node)) {
↓ open down ↓ 22 lines elided ↑ open up ↑
2142 2148           * <li> Invoke {@link #release} with
2143 2149           *      saved state as argument, throwing
2144 2150           *      IllegalMonitorStateException if it fails.
2145 2151           * <li> Block until signalled, interrupted, or timed out.
2146 2152           * <li> Reacquire by invoking specialized version of
2147 2153           *      {@link #acquire} with saved state as argument.
2148 2154           * <li> If interrupted while blocked in step 4, throw InterruptedException.
2149 2155           * <li> If timed out while blocked in step 4, return false, else true.
2150 2156           * </ol>
2151 2157           */
2152      -        public final boolean await(long time, TimeUnit unit) throws InterruptedException {
     2158 +        public final boolean await(long time, TimeUnit unit)
     2159 +                throws InterruptedException {
2153 2160              if (unit == null)
2154 2161                  throw new NullPointerException();
2155 2162              long nanosTimeout = unit.toNanos(time);
2156 2163              if (Thread.interrupted())
2157 2164                  throw new InterruptedException();
2158 2165              Node node = addConditionWaiter();
2159 2166              int savedState = fullyRelease(node);
2160 2167              long lastTime = System.nanoTime();
2161 2168              boolean timedout = false;
2162 2169              int interruptMode = 0;
↓ open down ↓ 135 lines elided ↑ open up ↑
2298 2305      /**
2299 2306       * CAS tail field. Used only by enq.
2300 2307       */
2301 2308      private final boolean compareAndSetTail(Node expect, Node update) {
2302 2309          return unsafe.compareAndSwapObject(this, tailOffset, expect, update);
2303 2310      }
2304 2311  
2305 2312      /**
2306 2313       * CAS waitStatus field of a node.
2307 2314       */
2308      -    private final static boolean compareAndSetWaitStatus(Node node,
     2315 +    private static final boolean compareAndSetWaitStatus(Node node,
2309 2316                                                           int expect,
2310 2317                                                           int update) {
2311 2318          return unsafe.compareAndSwapInt(node, waitStatusOffset,
2312 2319                                          expect, update);
2313 2320      }
2314 2321  
2315 2322      /**
2316 2323       * CAS next field of a node.
2317 2324       */
2318      -    private final static boolean compareAndSetNext(Node node,
     2325 +    private static final boolean compareAndSetNext(Node node,
2319 2326                                                     Node expect,
2320 2327                                                     Node update) {
2321 2328          return unsafe.compareAndSwapObject(node, nextOffset, expect, update);
2322 2329      }
2323 2330  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX