Print this page


Split Close
Expand all
Collapse all
          --- old/src/share/classes/java/util/concurrent/Exchanger.java
          +++ new/src/share/classes/java/util/concurrent/Exchanger.java
↓ open down ↓ 347 lines elided ↑ open up ↑
 348  348                       slot.compareAndSet(y, null)) {
 349  349                  Node you = (Node)y;               // Transfer item
 350  350                  if (you.compareAndSet(null, item)) {
 351  351                      LockSupport.unpark(you.waiter);
 352  352                      return you.item;
 353  353                  }                                 // Else cancelled; continue
 354  354              }
 355  355              else if (y == null &&                 // Try to occupy
 356  356                       slot.compareAndSet(null, me)) {
 357  357                  if (index == 0)                   // Blocking wait for slot 0
 358      -                    return timed? awaitNanos(me, slot, nanos): await(me, slot);
      358 +                    return timed ?
      359 +                        awaitNanos(me, slot, nanos) :
      360 +                        await(me, slot);
 359  361                  Object v = spinWait(me, slot);    // Spin wait for non-0
 360  362                  if (v != CANCEL)
 361  363                      return v;
 362  364                  me = new Node(item);              // Throw away cancelled node
 363  365                  int m = max.get();
 364  366                  if (m > (index >>>= 1))           // Decrease index
 365  367                      max.compareAndSet(m, m - 1);  // Maybe shrink table
 366  368              }
 367  369              else if (++fails > 1) {               // Allow 2 fails on 1st slot
 368  370                  int m = max.get();
↓ open down ↓ 221 lines elided ↑ open up ↑
 590  592       * <p>If another thread is already waiting at the exchange point then
 591  593       * it is resumed for thread scheduling purposes and receives the object
 592  594       * passed in by the current thread.  The current thread returns immediately,
 593  595       * receiving the object passed to the exchange by that other thread.
 594  596       *
 595  597       * <p>If no other thread is already waiting at the exchange then the
 596  598       * current thread is disabled for thread scheduling purposes and lies
 597  599       * dormant until one of two things happens:
 598  600       * <ul>
 599  601       * <li>Some other thread enters the exchange; or
 600      -     * <li>Some other thread {@linkplain Thread#interrupt interrupts} the current
 601      -     * thread.
      602 +     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
      603 +     * the current thread.
 602  604       * </ul>
 603  605       * <p>If the current thread:
 604  606       * <ul>
 605  607       * <li>has its interrupted status set on entry to this method; or
 606  608       * <li>is {@linkplain Thread#interrupt interrupted} while waiting
 607  609       * for the exchange,
 608  610       * </ul>
 609  611       * then {@link InterruptedException} is thrown and the current thread's
 610  612       * interrupted status is cleared.
 611  613       *
 612  614       * @param x the object to exchange
 613  615       * @return the object provided by the other thread
 614  616       * @throws InterruptedException if the current thread was
 615  617       *         interrupted while waiting
 616  618       */
 617  619      public V exchange(V x) throws InterruptedException {
 618  620          if (!Thread.interrupted()) {
 619      -            Object v = doExchange(x == null? NULL_ITEM : x, false, 0);
      621 +            Object v = doExchange((x == null) ? NULL_ITEM : x, false, 0);
 620  622              if (v == NULL_ITEM)
 621  623                  return null;
 622  624              if (v != CANCEL)
 623  625                  return (V)v;
 624  626              Thread.interrupted(); // Clear interrupt status on IE throw
 625  627          }
 626  628          throw new InterruptedException();
 627  629      }
 628  630  
 629  631      /**
↓ open down ↓ 34 lines elided ↑ open up ↑
 664  666       * @param unit the time unit of the <tt>timeout</tt> argument
 665  667       * @return the object provided by the other thread
 666  668       * @throws InterruptedException if the current thread was
 667  669       *         interrupted while waiting
 668  670       * @throws TimeoutException if the specified waiting time elapses
 669  671       *         before another thread enters the exchange
 670  672       */
 671  673      public V exchange(V x, long timeout, TimeUnit unit)
 672  674          throws InterruptedException, TimeoutException {
 673  675          if (!Thread.interrupted()) {
 674      -            Object v = doExchange(x == null? NULL_ITEM : x,
      676 +            Object v = doExchange((x == null) ? NULL_ITEM : x,
 675  677                                    true, unit.toNanos(timeout));
 676  678              if (v == NULL_ITEM)
 677  679                  return null;
 678  680              if (v != CANCEL)
 679  681                  return (V)v;
 680  682              if (!Thread.interrupted())
 681  683                  throw new TimeoutException();
 682  684          }
 683  685          throw new InterruptedException();
 684  686      }
 685  687  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX