| 47 |
* Java Collections Framework</a>. |
* Java Collections Framework</a>. |
| 48 |
* |
* |
| 49 |
* @since 1.5 |
* @since 1.5 |
| 50 |
* @author Doug Lea |
* @author Doug Lea and Bill Scherer and Michael Scott |
| 51 |
* @param <E> the type of elements held in this collection |
* @param <E> the type of elements held in this collection |
| 52 |
*/ |
*/ |
| 53 |
public class SynchronousQueue<E> extends AbstractQueue<E> |
public class SynchronousQueue<E> extends AbstractQueue<E> |
| 98 |
* of further adaptations. |
* of further adaptations. |
| 99 |
* 2. SynchronousQueues must block threads waiting to become |
* 2. SynchronousQueues must block threads waiting to become |
| 100 |
* fulfilled. |
* fulfilled. |
| 101 |
* 3. Nodes/threads that have been cancelled due to timeouts |
* 3. Support for cancellation via timeout and interrupts, |
| 102 |
* or interruptions are cleaned out of the lists to |
* including cleaning out cancelled nodes/threads |
| 103 |
* avoid garbage retention and memory depletion. |
* from lists to avoid garbage retention and memory depletion. |
| 104 |
* |
* |
| 105 |
* Blocking is mainly accomplished using LockSupport park/unpark, |
* Blocking is mainly accomplished using LockSupport park/unpark, |
| 106 |
* except that nodes that appear to be the next ones to become |
* except that nodes that appear to be the next ones to become |
| 156 |
/** |
/** |
| 157 |
* The number of times to spin before blocking in timed waits. |
* The number of times to spin before blocking in timed waits. |
| 158 |
* The value is empirically derived -- it works well across a |
* The value is empirically derived -- it works well across a |
| 159 |
* variety of processors and OSes. Emprically, the best value |
* variety of processors and OSes. Empirically, the best value |
| 160 |
* seems not to vary with number of CPUs (beyond 2) so is just |
* seems not to vary with number of CPUs (beyond 2) so is just |
| 161 |
* a constant. |
* a constant. |
| 162 |
*/ |
*/ |
| 430 |
*/ |
*/ |
| 431 |
boolean shouldSpin(SNode s) { |
boolean shouldSpin(SNode s) { |
| 432 |
SNode h = head; |
SNode h = head; |
| 433 |
return (h == null || h == s || isFulfilling(h.mode)); |
return (h == s || h == null || isFulfilling(h.mode)); |
| 434 |
} |
} |
| 435 |
|
|
| 436 |
/** |
/** |
| 522 |
boolean isCancelled() { |
boolean isCancelled() { |
| 523 |
return item == this; |
return item == this; |
| 524 |
} |
} |
| 525 |
|
|
| 526 |
|
/** |
| 527 |
|
* Return true if this node is known to be off the queue |
| 528 |
|
* because its next pointer has been forgotten due to |
| 529 |
|
* an advanceHead operation. |
| 530 |
|
*/ |
| 531 |
|
boolean isOffList() { |
| 532 |
|
return next == this; |
| 533 |
|
} |
| 534 |
} |
} |
| 535 |
|
|
| 536 |
/** Head of queue */ |
/** Head of queue */ |
| 647 |
return null; |
return null; |
| 648 |
} |
} |
| 649 |
|
|
| 650 |
if (s.next != s) { // not already unlinked |
if (!s.isOffList()) { // not already unlinked |
| 651 |
advanceHead(t, s); // unlink |
advanceHead(t, s); // unlink if head |
| 652 |
if (x != null) // and forget fields |
if (x != null) // and forget fields |
| 653 |
s.item = s; |
s.item = s; |
| 654 |
s.waiter = null; |
s.waiter = null; |
| 773 |
/** |
/** |
| 774 |
* The transferer. Set only in constructor, but cannot be declared |
* The transferer. Set only in constructor, but cannot be declared |
| 775 |
* as final without further complicating serialization. Since |
* as final without further complicating serialization. Since |
| 776 |
* this is accessed only once per public method, there isn't a |
* this is accessed only at most once per public method, there |
| 777 |
* noticeable performance penalty for using volatile instead of |
* isn't a noticeable performance penalty for using volatile |
| 778 |
* final here. |
* instead of final here. |
| 779 |
*/ |
*/ |
| 780 |
private transient volatile Transferer transferer; |
private transient volatile Transferer transferer; |
| 781 |
|
|