jdk/src/share/classes/java/util/concurrent/Exchanger.java
changeset 7518 0282db800fe1
parent 5506 202f599c92aa
child 8764 f34ee71fc38a
equal deleted inserted replaced
7517:7303bc0e78d6 7518:0282db800fe1
   353                 }                                 // Else cancelled; continue
   353                 }                                 // Else cancelled; continue
   354             }
   354             }
   355             else if (y == null &&                 // Try to occupy
   355             else if (y == null &&                 // Try to occupy
   356                      slot.compareAndSet(null, me)) {
   356                      slot.compareAndSet(null, me)) {
   357                 if (index == 0)                   // Blocking wait for slot 0
   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                 Object v = spinWait(me, slot);    // Spin wait for non-0
   361                 Object v = spinWait(me, slot);    // Spin wait for non-0
   360                 if (v != CANCEL)
   362                 if (v != CANCEL)
   361                     return v;
   363                     return v;
   362                 me = new Node(item);              // Throw away cancelled node
   364                 me = new Node(item);              // Throw away cancelled node
   363                 int m = max.get();
   365                 int m = max.get();
   595      * <p>If no other thread is already waiting at the exchange then the
   597      * <p>If no other thread is already waiting at the exchange then the
   596      * current thread is disabled for thread scheduling purposes and lies
   598      * current thread is disabled for thread scheduling purposes and lies
   597      * dormant until one of two things happens:
   599      * dormant until one of two things happens:
   598      * <ul>
   600      * <ul>
   599      * <li>Some other thread enters the exchange; or
   601      * <li>Some other thread enters the exchange; or
   600      * <li>Some other thread {@linkplain Thread#interrupt interrupts} the current
   602      * <li>Some other thread {@linkplain Thread#interrupt interrupts}
   601      * thread.
   603      * the current thread.
   602      * </ul>
   604      * </ul>
   603      * <p>If the current thread:
   605      * <p>If the current thread:
   604      * <ul>
   606      * <ul>
   605      * <li>has its interrupted status set on entry to this method; or
   607      * <li>has its interrupted status set on entry to this method; or
   606      * <li>is {@linkplain Thread#interrupt interrupted} while waiting
   608      * <li>is {@linkplain Thread#interrupt interrupted} while waiting
   614      * @throws InterruptedException if the current thread was
   616      * @throws InterruptedException if the current thread was
   615      *         interrupted while waiting
   617      *         interrupted while waiting
   616      */
   618      */
   617     public V exchange(V x) throws InterruptedException {
   619     public V exchange(V x) throws InterruptedException {
   618         if (!Thread.interrupted()) {
   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             if (v == NULL_ITEM)
   622             if (v == NULL_ITEM)
   621                 return null;
   623                 return null;
   622             if (v != CANCEL)
   624             if (v != CANCEL)
   623                 return (V)v;
   625                 return (V)v;
   624             Thread.interrupted(); // Clear interrupt status on IE throw
   626             Thread.interrupted(); // Clear interrupt status on IE throw
   669      *         before another thread enters the exchange
   671      *         before another thread enters the exchange
   670      */
   672      */
   671     public V exchange(V x, long timeout, TimeUnit unit)
   673     public V exchange(V x, long timeout, TimeUnit unit)
   672         throws InterruptedException, TimeoutException {
   674         throws InterruptedException, TimeoutException {
   673         if (!Thread.interrupted()) {
   675         if (!Thread.interrupted()) {
   674             Object v = doExchange(x == null? NULL_ITEM : x,
   676             Object v = doExchange((x == null) ? NULL_ITEM : x,
   675                                   true, unit.toNanos(timeout));
   677                                   true, unit.toNanos(timeout));
   676             if (v == NULL_ITEM)
   678             if (v == NULL_ITEM)
   677                 return null;
   679                 return null;
   678             if (v != CANCEL)
   680             if (v != CANCEL)
   679                 return (V)v;
   681                 return (V)v;