<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_Tutorial%2FCollections%2FQueue</id>
		<title>Java Tutorial/Collections/Queue - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_Tutorial%2FCollections%2FQueue"/>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_Tutorial/Collections/Queue&amp;action=history"/>
		<updated>2026-04-22T22:46:34Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java_Tutorial/Collections/Queue&amp;diff=4676&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_Tutorial/Collections/Queue&amp;diff=4676&amp;oldid=prev"/>
				<updated>2010-06-01T05:04:16Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 05:04, 1 июня 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java_Tutorial/Collections/Queue&amp;diff=4675&amp;oldid=prev</id>
		<title> в 17:44, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_Tutorial/Collections/Queue&amp;diff=4675&amp;oldid=prev"/>
				<updated>2010-05-31T17:44:27Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==  A Priority Queue ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Licensed to the Apache Software Foundation (ASF) under one or more&lt;br /&gt;
 * contributor license agreements.  See the NOTICE file distributed with&lt;br /&gt;
 * this work for additional information regarding copyright ownership.&lt;br /&gt;
 * The ASF licenses this file to You under the Apache License, Version 2.0&lt;br /&gt;
 * (the &amp;quot;License&amp;quot;); you may not use this file except in compliance with&lt;br /&gt;
 * the License.  You may obtain a copy of the License at&lt;br /&gt;
 *&lt;br /&gt;
 *     http://www.apache.org/licenses/LICENSE-2.0&lt;br /&gt;
 *&lt;br /&gt;
 * Unless required by applicable law or agreed to in writing, software&lt;br /&gt;
 * distributed under the License is distributed on an &amp;quot;AS IS&amp;quot; BASIS,&lt;br /&gt;
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.&lt;br /&gt;
 * See the License for the specific language governing permissions and&lt;br /&gt;
 * limitations under the License.&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
/** A PriorityQueue maintains a partial ordering of its elements such that the&lt;br /&gt;
  least element can always be found in constant time.  Put()&amp;quot;s and pop()&amp;quot;s&lt;br /&gt;
  require log(size) time. */&lt;br /&gt;
public abstract class PriorityQueue {&lt;br /&gt;
  private Object[] heap;&lt;br /&gt;
  private int size;&lt;br /&gt;
  private int maxSize;&lt;br /&gt;
  /** Determines the ordering of objects in this priority queue.  Subclasses&lt;br /&gt;
      must define this one method. */&lt;br /&gt;
  protected abstract boolean lessThan(Object a, Object b);&lt;br /&gt;
  /** Subclass constructors must call this. */&lt;br /&gt;
  protected final void initialize(int maxSize) {&lt;br /&gt;
    size = 0;&lt;br /&gt;
    int heapSize = maxSize + 1;&lt;br /&gt;
    heap = new Object[heapSize];&lt;br /&gt;
    this.maxSize = maxSize;&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Adds an Object to a PriorityQueue in log(size) time.&lt;br /&gt;
   * If one tries to add more objects than maxSize from initialize&lt;br /&gt;
   * a RuntimeException (ArrayIndexOutOfBound) is thrown.&lt;br /&gt;
   */&lt;br /&gt;
  public final void put(Object element) {&lt;br /&gt;
    size++;&lt;br /&gt;
    heap[size] = element;&lt;br /&gt;
    upHeap();&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Adds element to the PriorityQueue in log(size) time if either&lt;br /&gt;
   * the PriorityQueue is not full, or not lessThan(element, top()).&lt;br /&gt;
   * @param element&lt;br /&gt;
   * @return true if element is added, false otherwise.&lt;br /&gt;
   */&lt;br /&gt;
  public boolean insert(Object element){&lt;br /&gt;
    if (size &amp;lt; maxSize){&lt;br /&gt;
      put(element);&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
    else if (size &amp;gt; 0 &amp;amp;&amp;amp; !lessThan(element, top())){&lt;br /&gt;
      heap[1] = element;&lt;br /&gt;
      adjustTop();&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
    else&lt;br /&gt;
      return false;&lt;br /&gt;
  }&lt;br /&gt;
  /** Returns the least element of the PriorityQueue in constant time. */&lt;br /&gt;
  public final Object top() {&lt;br /&gt;
    if (size &amp;gt; 0)&lt;br /&gt;
      return heap[1];&lt;br /&gt;
    else&lt;br /&gt;
      return null;&lt;br /&gt;
  }&lt;br /&gt;
  /** Removes and returns the least element of the PriorityQueue in log(size)&lt;br /&gt;
      time. */&lt;br /&gt;
  public final Object pop() {&lt;br /&gt;
    if (size &amp;gt; 0) {&lt;br /&gt;
      Object result = heap[1];        // save first value&lt;br /&gt;
      heap[1] = heap[size];       // move last to first&lt;br /&gt;
      heap[size] = null;        // permit GC of objects&lt;br /&gt;
      size--;&lt;br /&gt;
      downHeap();         // adjust heap&lt;br /&gt;
      return result;&lt;br /&gt;
    } else&lt;br /&gt;
      return null;&lt;br /&gt;
  }&lt;br /&gt;
  /** Should be called when the Object at top changes values.  Still log(n)&lt;br /&gt;
   * worst case, but it&amp;quot;s at least twice as fast to &amp;lt;pre&amp;gt;&lt;br /&gt;
   *  { pq.top().change(); pq.adjustTop(); }&lt;br /&gt;
   * &amp;lt;/pre&amp;gt; instead of &amp;lt;pre&amp;gt;&lt;br /&gt;
   *  { o = pq.pop(); o.change(); pq.push(o); }&lt;br /&gt;
   * &amp;lt;/pre&amp;gt;&lt;br /&gt;
   */&lt;br /&gt;
  public final void adjustTop() {&lt;br /&gt;
    downHeap();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Returns the number of elements currently stored in the PriorityQueue. */&lt;br /&gt;
  public final int size() {&lt;br /&gt;
    return size;&lt;br /&gt;
  }&lt;br /&gt;
  /** Removes all entries from the PriorityQueue. */&lt;br /&gt;
  public final void clear() {&lt;br /&gt;
    for (int i = 0; i &amp;lt;= size; i++)&lt;br /&gt;
      heap[i] = null;&lt;br /&gt;
    size = 0;&lt;br /&gt;
  }&lt;br /&gt;
  private final void upHeap() {&lt;br /&gt;
    int i = size;&lt;br /&gt;
    Object node = heap[i];        // save bottom node&lt;br /&gt;
    int j = i &amp;gt;&amp;gt;&amp;gt; 1;&lt;br /&gt;
    while (j &amp;gt; 0 &amp;amp;&amp;amp; lessThan(node, heap[j])) {&lt;br /&gt;
      heap[i] = heap[j];        // shift parents down&lt;br /&gt;
      i = j;&lt;br /&gt;
      j = j &amp;gt;&amp;gt;&amp;gt; 1;&lt;br /&gt;
    }&lt;br /&gt;
    heap[i] = node;         // install saved node&lt;br /&gt;
  }&lt;br /&gt;
  private final void downHeap() {&lt;br /&gt;
    int i = 1;&lt;br /&gt;
    Object node = heap[i];        // save top node&lt;br /&gt;
    int j = i &amp;lt;&amp;lt; 1;         // find smaller child&lt;br /&gt;
    int k = j + 1;&lt;br /&gt;
    if (k &amp;lt;= size &amp;amp;&amp;amp; lessThan(heap[k], heap[j])) {&lt;br /&gt;
      j = k;&lt;br /&gt;
    }&lt;br /&gt;
    while (j &amp;lt;= size &amp;amp;&amp;amp; lessThan(heap[j], node)) {&lt;br /&gt;
      heap[i] = heap[j];        // shift up child&lt;br /&gt;
      i = j;&lt;br /&gt;
      j = i &amp;lt;&amp;lt; 1;&lt;br /&gt;
      k = j + 1;&lt;br /&gt;
      if (k &amp;lt;= size &amp;amp;&amp;amp; lessThan(heap[k], heap[j])) {&lt;br /&gt;
  j = k;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    heap[i] = node;         // install saved node&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Binary Heap Queue ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright 2005 JBoss Inc&lt;br /&gt;
 *&lt;br /&gt;
 * Licensed under the Apache License, Version 2.0 (the &amp;quot;License&amp;quot;);&lt;br /&gt;
 * you may not use this file except in compliance with the License.&lt;br /&gt;
 * You may obtain a copy of the License at&lt;br /&gt;
 *&lt;br /&gt;
 *      http://www.apache.org/licenses/LICENSE-2.0&lt;br /&gt;
 *&lt;br /&gt;
 * Unless required by applicable law or agreed to in writing, software&lt;br /&gt;
 * distributed under the License is distributed on an &amp;quot;AS IS&amp;quot; BASIS,&lt;br /&gt;
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.&lt;br /&gt;
 * See the License for the specific language governing permissions and&lt;br /&gt;
 * limitations under the License.&lt;br /&gt;
 */&lt;br /&gt;
import java.io.Externalizable;&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.io.ObjectInput;&lt;br /&gt;
import java.io.ObjectOutput;&lt;br /&gt;
import java.util.ruparator;&lt;br /&gt;
import java.util.NoSuchElementException;&lt;br /&gt;
public class BinaryHeapQueue implements Queue, Externalizable {&lt;br /&gt;
  /** The default capacity for a binary heap. */&lt;br /&gt;
  private final static int DEFAULT_CAPACITY = 13;&lt;br /&gt;
  /** The comparator used to order the elements */&lt;br /&gt;
  private Comparator comparator;&lt;br /&gt;
  /** The number of elements currently in this heap. */&lt;br /&gt;
  private int size;&lt;br /&gt;
  /** The elements in this heap. */&lt;br /&gt;
  private Queueable[] elements;&lt;br /&gt;
  public BinaryHeapQueue() {&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Constructs a new &amp;lt;code&amp;gt;BinaryHeap&amp;lt;/code&amp;gt; that will use the given&lt;br /&gt;
   * comparator to order its elements.&lt;br /&gt;
   * &lt;br /&gt;
   * @param comparator&lt;br /&gt;
   *          the comparator used to order the elements, null means use natural&lt;br /&gt;
   *          order&lt;br /&gt;
   */&lt;br /&gt;
  public BinaryHeapQueue(final Comparator comparator) {&lt;br /&gt;
    this(comparator, BinaryHeapQueue.DEFAULT_CAPACITY);&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Constructs a new &amp;lt;code&amp;gt;BinaryHeap&amp;lt;/code&amp;gt;.&lt;br /&gt;
   * &lt;br /&gt;
   * @param comparator&lt;br /&gt;
   *          the comparator used to order the elements, null means use natural&lt;br /&gt;
   *          order&lt;br /&gt;
   * @param capacity&lt;br /&gt;
   *          the initial capacity for the heap&lt;br /&gt;
   * @throws IllegalArgumentException&lt;br /&gt;
   *           if &amp;lt;code&amp;gt;capacity&amp;lt;/code&amp;gt; is &amp;amp;lt;= &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt;&lt;br /&gt;
   */&lt;br /&gt;
  public BinaryHeapQueue(final Comparator comparator, final int capacity) {&lt;br /&gt;
    if (capacity &amp;lt;= 0) {&lt;br /&gt;
      throw new IllegalArgumentException(&amp;quot;invalid capacity&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    // +1 as 0 is noop&lt;br /&gt;
    this.elements = new Queueable[capacity + 1];&lt;br /&gt;
    this.ruparator = comparator;&lt;br /&gt;
  }&lt;br /&gt;
  // -----------------------------------------------------------------------&lt;br /&gt;
  public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {&lt;br /&gt;
    comparator = (Comparator) in.readObject();&lt;br /&gt;
    elements = (Queueable[]) in.readObject();&lt;br /&gt;
    size = in.readInt();&lt;br /&gt;
  }&lt;br /&gt;
  public void writeExternal(ObjectOutput out) throws IOException {&lt;br /&gt;
    out.writeObject(comparator);&lt;br /&gt;
    out.writeObject(elements);&lt;br /&gt;
    out.writeInt(size);&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Clears all elements from queue.&lt;br /&gt;
   */&lt;br /&gt;
  public void clear() {&lt;br /&gt;
    this.elements = new Queueable[this.elements.length]; // for gc&lt;br /&gt;
    this.size = 0;&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Tests if queue is empty.&lt;br /&gt;
   * &lt;br /&gt;
   * @return &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt; if queue is empty; &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;&lt;br /&gt;
   *         otherwise.&lt;br /&gt;
   */&lt;br /&gt;
  public boolean isEmpty() {&lt;br /&gt;
    return this.size == 0;&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Tests if queue is full.&lt;br /&gt;
   * &lt;br /&gt;
   * @return &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt; if queue is full; &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;&lt;br /&gt;
   *         otherwise.&lt;br /&gt;
   */&lt;br /&gt;
  public boolean isFull() {&lt;br /&gt;
    // +1 as Queueable 0 is noop&lt;br /&gt;
    return this.elements.length == this.size + 1;&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Returns the number of elements in this heap.&lt;br /&gt;
   * &lt;br /&gt;
   * @return the number of elements in this heap&lt;br /&gt;
   */&lt;br /&gt;
  public int size() {&lt;br /&gt;
    return this.size;&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Inserts an Queueable into queue.&lt;br /&gt;
   * &lt;br /&gt;
   * @param element&lt;br /&gt;
   *          the Queueable to be inserted&lt;br /&gt;
   */&lt;br /&gt;
  public synchronized void enqueue(final Queueable element) {&lt;br /&gt;
    if (isFull()) {&lt;br /&gt;
      grow();&lt;br /&gt;
    }&lt;br /&gt;
    percolateUpMinHeap(element);&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Returns the Queueable on top of heap and remove it.&lt;br /&gt;
   * &lt;br /&gt;
   * @return the Queueable at top of heap&lt;br /&gt;
   * @throws NoSuchElementException&lt;br /&gt;
   *           if &amp;lt;code&amp;gt;isEmpty() == true&amp;lt;/code&amp;gt;&lt;br /&gt;
   */&lt;br /&gt;
  public synchronized Queueable dequeue() throws NoSuchElementException {&lt;br /&gt;
    if (isEmpty()) {&lt;br /&gt;
      return null;&lt;br /&gt;
    }&lt;br /&gt;
    final Queueable result = this.elements[1];&lt;br /&gt;
    result.dequeue();&lt;br /&gt;
    // Code bellow was removed because it is already executed&lt;br /&gt;
    // inside result.dequeue()&lt;br /&gt;
    //&lt;br /&gt;
    // setElement(1, this.elements[this.size--]);&lt;br /&gt;
    // this.elements[this.size + 1] = null;&lt;br /&gt;
    //&lt;br /&gt;
    // if (this.size != 0) {&lt;br /&gt;
    // percolateDownMinHeap(1);&lt;br /&gt;
    // }&lt;br /&gt;
    return result;&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * &lt;br /&gt;
   * @param index&lt;br /&gt;
   */&lt;br /&gt;
  public synchronized Queueable dequeue(final int index) {&lt;br /&gt;
    if (index &amp;lt; 1 || index &amp;gt; this.size) {&lt;br /&gt;
      // throw new NoSuchElementException();&lt;br /&gt;
      return null;&lt;br /&gt;
    }&lt;br /&gt;
    final Queueable result = this.elements[index];&lt;br /&gt;
    setElement(index, this.elements[this.size]);&lt;br /&gt;
    this.elements[this.size] = null;&lt;br /&gt;
    this.size--;&lt;br /&gt;
    if (this.size != 0 &amp;amp;&amp;amp; index &amp;lt;= this.size) {&lt;br /&gt;
      int compareToParent = 0;&lt;br /&gt;
      if (index &amp;gt; 1) {&lt;br /&gt;
        compareToParent = compare(this.elements[index], this.elements[index / 2]);&lt;br /&gt;
      }&lt;br /&gt;
      if (index &amp;gt; 1 &amp;amp;&amp;amp; compareToParent &amp;lt; 0) {&lt;br /&gt;
        percolateUpMinHeap(index);&lt;br /&gt;
      } else {&lt;br /&gt;
        percolateDownMinHeap(index);&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    return result;&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Percolates Queueable down heap from the position given by the index. &amp;lt;p/&amp;gt;&lt;br /&gt;
   * Assumes it is a minimum heap.&lt;br /&gt;
   * &lt;br /&gt;
   * @param index&lt;br /&gt;
   *          the index for the Queueable&lt;br /&gt;
   */&lt;br /&gt;
  private void percolateDownMinHeap(final int index) {&lt;br /&gt;
    final Queueable element = this.elements[index];&lt;br /&gt;
    int hole = index;&lt;br /&gt;
    while ((hole * 2) &amp;lt;= this.size) {&lt;br /&gt;
      int child = hole * 2;&lt;br /&gt;
      // if we have a right child and that child can not be percolated&lt;br /&gt;
      // up then move onto other child&lt;br /&gt;
      if (child != this.size &amp;amp;&amp;amp; compare(this.elements[child + 1], this.elements[child]) &amp;lt; 0) {&lt;br /&gt;
        child++;&lt;br /&gt;
      }&lt;br /&gt;
      // if we found resting place of bubble then terminate search&lt;br /&gt;
      if (compare(this.elements[child], element) &amp;gt;= 0) {&lt;br /&gt;
        break;&lt;br /&gt;
      }&lt;br /&gt;
      setElement(hole, this.elements[child]);&lt;br /&gt;
      hole = child;&lt;br /&gt;
    }&lt;br /&gt;
    setElement(hole, element);&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Percolates Queueable up heap from the position given by the index. &amp;lt;p/&amp;gt;&lt;br /&gt;
   * Assumes it is a minimum heap.&lt;br /&gt;
   * &lt;br /&gt;
   * @param index&lt;br /&gt;
   *          the index of the Queueable to be percolated up&lt;br /&gt;
   */&lt;br /&gt;
  private void percolateUpMinHeap(final int index) {&lt;br /&gt;
    int hole = index;&lt;br /&gt;
    final Queueable element = this.elements[hole];&lt;br /&gt;
    while (hole &amp;gt; 1 &amp;amp;&amp;amp; compare(element, this.elements[hole / 2]) &amp;lt; 0) {&lt;br /&gt;
      // save Queueable that is being pushed down&lt;br /&gt;
      // as the Queueable &amp;quot;bubble&amp;quot; is percolated up&lt;br /&gt;
      final int next = hole / 2;&lt;br /&gt;
      setElement(hole, this.elements[next]);&lt;br /&gt;
      hole = next;&lt;br /&gt;
    }&lt;br /&gt;
    setElement(hole, element);&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Percolates a new Queueable up heap from the bottom. &amp;lt;p/&amp;gt; Assumes it is a&lt;br /&gt;
   * minimum heap.&lt;br /&gt;
   * &lt;br /&gt;
   * @param element&lt;br /&gt;
   *          the Queueable&lt;br /&gt;
   */&lt;br /&gt;
  private void percolateUpMinHeap(final Queueable element) {&lt;br /&gt;
    setElement(++this.size, element);&lt;br /&gt;
    percolateUpMinHeap(this.size);&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Compares two objects using the comparator if specified, or the natural&lt;br /&gt;
   * order otherwise.&lt;br /&gt;
   * &lt;br /&gt;
   * @param a&lt;br /&gt;
   *          the first object&lt;br /&gt;
   * @param b&lt;br /&gt;
   *          the second object&lt;br /&gt;
   * @return -ve if a less than b, 0 if they are equal, +ve if a greater than b&lt;br /&gt;
   */&lt;br /&gt;
  private int compare(final Queueable a, final Queueable b) {&lt;br /&gt;
    return this.ruparator.rupare(a, b);&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Increases the size of the heap to support additional elements&lt;br /&gt;
   */&lt;br /&gt;
  private void grow() {&lt;br /&gt;
    final Queueable[] elements = new Queueable[this.elements.length * 2];&lt;br /&gt;
    System.arraycopy(this.elements, 0, elements, 0, this.elements.length);&lt;br /&gt;
    this.elements = elements;&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * &lt;br /&gt;
   * @param index&lt;br /&gt;
   * @param element&lt;br /&gt;
   */&lt;br /&gt;
  private void setElement(final int index, final Queueable element) {&lt;br /&gt;
    this.elements[index] = element;&lt;br /&gt;
    element.enqueued(this, index);&lt;br /&gt;
  }&lt;br /&gt;
  public Queueable[] getQueueable() {&lt;br /&gt;
    return this.elements;&lt;br /&gt;
  }&lt;br /&gt;
  public Object[] toArray() {&lt;br /&gt;
    final Object[] result = new Object[this.size];&lt;br /&gt;
    System.arraycopy(this.elements, 1, result, 0, this.size);&lt;br /&gt;
    return result;&lt;br /&gt;
  }&lt;br /&gt;
  public Object[] toArray(Object a[]) {&lt;br /&gt;
    if (a.length &amp;lt; this.size) {&lt;br /&gt;
      a = (Object[]) java.lang.reflect.Array&lt;br /&gt;
          .newInstance(a.getClass().getComponentType(), this.size);&lt;br /&gt;
    }&lt;br /&gt;
    System.arraycopy(this.elements, 1, a, 0, this.size);&lt;br /&gt;
    if (a.length &amp;gt; this.size) {&lt;br /&gt;
      a[this.size] = null;&lt;br /&gt;
    }&lt;br /&gt;
    return a;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright 2005 JBoss Inc&lt;br /&gt;
 * &lt;br /&gt;
 * Licensed under the Apache License, Version 2.0 (the &amp;quot;License&amp;quot;); you may not&lt;br /&gt;
 * use this file except in compliance with the License. You may obtain a copy of&lt;br /&gt;
 * the License at&lt;br /&gt;
 * &lt;br /&gt;
 * http://www.apache.org/licenses/LICENSE-2.0&lt;br /&gt;
 * &lt;br /&gt;
 * Unless required by applicable law or agreed to in writing, software&lt;br /&gt;
 * distributed under the License is distributed on an &amp;quot;AS IS&amp;quot; BASIS, WITHOUT&lt;br /&gt;
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the&lt;br /&gt;
 * License for the specific language governing permissions and limitations under&lt;br /&gt;
 * the License.&lt;br /&gt;
 */&lt;br /&gt;
interface Queue {&lt;br /&gt;
  public void enqueue(Queueable queueable);&lt;br /&gt;
  public Queueable dequeue();&lt;br /&gt;
  public Queueable dequeue(int index);&lt;br /&gt;
  public boolean isEmpty();&lt;br /&gt;
}&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright 2005 JBoss Inc&lt;br /&gt;
 * &lt;br /&gt;
 * Licensed under the Apache License, Version 2.0 (the &amp;quot;License&amp;quot;); you may not&lt;br /&gt;
 * use this file except in compliance with the License. You may obtain a copy of&lt;br /&gt;
 * the License at&lt;br /&gt;
 * &lt;br /&gt;
 * http://www.apache.org/licenses/LICENSE-2.0&lt;br /&gt;
 * &lt;br /&gt;
 * Unless required by applicable law or agreed to in writing, software&lt;br /&gt;
 * distributed under the License is distributed on an &amp;quot;AS IS&amp;quot; BASIS, WITHOUT&lt;br /&gt;
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the&lt;br /&gt;
 * License for the specific language governing permissions and limitations under&lt;br /&gt;
 * the License.&lt;br /&gt;
 */&lt;br /&gt;
interface Queueable {&lt;br /&gt;
  public void enqueued(Queue queue, int index);&lt;br /&gt;
  public void dequeue();&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Checking what item is first in line without removing it: element ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.util.LinkedList;&lt;br /&gt;
import java.util.Queue;&lt;br /&gt;
public class Main {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        Queue&amp;lt;String&amp;gt; queue = new LinkedList&amp;lt;String&amp;gt;();&lt;br /&gt;
        &lt;br /&gt;
        queue.add(&amp;quot;A&amp;quot;);&lt;br /&gt;
        queue.add(&amp;quot;B&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
        queue.offer(&amp;quot;C&amp;quot;);&lt;br /&gt;
        queue.offer(&amp;quot;D&amp;quot;);&lt;br /&gt;
        System.out.println(&amp;quot;remove: &amp;quot; + queue.remove());&lt;br /&gt;
        System.out.println(&amp;quot;element: &amp;quot; + queue.element());&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot;poll: &amp;quot; + queue.poll());&lt;br /&gt;
        System.out.println(&amp;quot;peek: &amp;quot; + queue.peek());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
/*&lt;br /&gt;
remove: A&lt;br /&gt;
element: B&lt;br /&gt;
poll: B&lt;br /&gt;
peek: C&lt;br /&gt;
*/&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Circular Queue ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/*   Copyright 2004 BEA Systems, Inc.&lt;br /&gt;
 *&lt;br /&gt;
 *   Licensed under the Apache License, Version 2.0 (the &amp;quot;License&amp;quot;);&lt;br /&gt;
 *   you may not use this file except in compliance with the License.&lt;br /&gt;
 *   You may obtain a copy of the License at&lt;br /&gt;
 *&lt;br /&gt;
 *       http://www.apache.org/licenses/LICENSE-2.0&lt;br /&gt;
 *&lt;br /&gt;
 *   Unless required by applicable law or agreed to in writing, software&lt;br /&gt;
 *   distributed under the License is distributed on an &amp;quot;AS IS&amp;quot; BASIS,&lt;br /&gt;
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.&lt;br /&gt;
 *   See the License for the specific language governing permissions and&lt;br /&gt;
 *   limitations under the License.&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
import java.util.AbstractCollection;&lt;br /&gt;
import java.util.Arrays;&lt;br /&gt;
import java.util.ConcurrentModificationException;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.NoSuchElementException;&lt;br /&gt;
public final class CircularQueue extends AbstractCollection {&lt;br /&gt;
  // This is the largest capacity allowed by this implementation&lt;br /&gt;
  private static final int MAX_CAPACITY = 1 &amp;lt;&amp;lt; 30;&lt;br /&gt;
  private static final int DEFAULT_CAPACITY = 1 &amp;lt;&amp;lt; 8;&lt;br /&gt;
  private int size          = 0;&lt;br /&gt;
  private int producerIndex = 0;&lt;br /&gt;
  private int consumerIndex = 0;&lt;br /&gt;
  // capacity must be a power of 2 at all times&lt;br /&gt;
  private int capacity;&lt;br /&gt;
  private int maxCapacity;&lt;br /&gt;
  // we mask with capacity -1.  This variable caches that values&lt;br /&gt;
  private int bitmask; &lt;br /&gt;
  private Object[] q;&lt;br /&gt;
  public CircularQueue() {&lt;br /&gt;
    this(DEFAULT_CAPACITY);&lt;br /&gt;
  }&lt;br /&gt;
  // Construct a queue which has at least the specified capacity.  If&lt;br /&gt;
  // the value specified is a power of two then the queue will be&lt;br /&gt;
  // exactly the specified size.  Otherwise the queue will be the&lt;br /&gt;
  // first power of two which is greater than the specified value.&lt;br /&gt;
  public CircularQueue(int c) {&lt;br /&gt;
    this(c, MAX_CAPACITY);&lt;br /&gt;
  }&lt;br /&gt;
  public CircularQueue(int c, int mc) {&lt;br /&gt;
    if (c &amp;gt; mc) {&lt;br /&gt;
      throw new IllegalArgumentException(&amp;quot;Capacity greater than maximum&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    if (mc &amp;gt; MAX_CAPACITY) {&lt;br /&gt;
      throw new IllegalArgumentException(&amp;quot;Maximum capacity greater than &amp;quot; +&lt;br /&gt;
        &amp;quot;allowed&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    for (capacity = 1; capacity &amp;lt; c; capacity &amp;lt;&amp;lt;= 1) ;&lt;br /&gt;
    for (maxCapacity = 1; maxCapacity &amp;lt; mc; maxCapacity &amp;lt;&amp;lt;= 1) ;&lt;br /&gt;
    bitmask = capacity - 1;&lt;br /&gt;
    q = new Object[capacity];&lt;br /&gt;
  }&lt;br /&gt;
  // Constructor used by clone()&lt;br /&gt;
  private CircularQueue(CircularQueue oldQueue) {&lt;br /&gt;
    size = oldQueue.size;&lt;br /&gt;
    producerIndex = oldQueue.producerIndex;&lt;br /&gt;
    consumerIndex = oldQueue.consumerIndex;&lt;br /&gt;
    capacity = oldQueue.capacity;&lt;br /&gt;
    maxCapacity = oldQueue.maxCapacity;&lt;br /&gt;
    bitmask = oldQueue.bitmask;&lt;br /&gt;
    q = new Object[oldQueue.q.length];&lt;br /&gt;
    System.arraycopy(oldQueue.q, 0, q, 0, q.length);&lt;br /&gt;
  }&lt;br /&gt;
  private boolean expandQueue() {&lt;br /&gt;
    // double the size of the queue&lt;br /&gt;
    // This design assumes that this is a rare case&lt;br /&gt;
    if (capacity == maxCapacity) {&lt;br /&gt;
      return false;&lt;br /&gt;
    }&lt;br /&gt;
    int old_capacity = capacity;&lt;br /&gt;
    Object[] old_q    = q;&lt;br /&gt;
    capacity += capacity;&lt;br /&gt;
    bitmask   = capacity - 1;&lt;br /&gt;
    q         = new Object[capacity];&lt;br /&gt;
    System.arraycopy(old_q, consumerIndex, q, 0, old_capacity - consumerIndex);&lt;br /&gt;
    if (consumerIndex != 0) {&lt;br /&gt;
      System.arraycopy(old_q, 0, q, old_capacity - consumerIndex, &lt;br /&gt;
        consumerIndex);&lt;br /&gt;
    }&lt;br /&gt;
    consumerIndex = 0;&lt;br /&gt;
    producerIndex = size;&lt;br /&gt;
    return true;&lt;br /&gt;
  }&lt;br /&gt;
  public boolean add(Object obj) {&lt;br /&gt;
    if (size == capacity) {&lt;br /&gt;
      // no room&lt;br /&gt;
      if (!expandQueue()) return false;&lt;br /&gt;
    }&lt;br /&gt;
    size++;&lt;br /&gt;
    q[producerIndex] = obj;&lt;br /&gt;
    producerIndex = (producerIndex + 1) &amp;amp; bitmask;&lt;br /&gt;
    return true;&lt;br /&gt;
  }&lt;br /&gt;
  public Object remove() {&lt;br /&gt;
    Object obj;&lt;br /&gt;
    &lt;br /&gt;
    if (size == 0) return null;&lt;br /&gt;
    &lt;br /&gt;
    size--;&lt;br /&gt;
    obj = q[consumerIndex];&lt;br /&gt;
    q[consumerIndex] = null; // allow gc to collect&lt;br /&gt;
    &lt;br /&gt;
    consumerIndex = (consumerIndex + 1) &amp;amp; bitmask;&lt;br /&gt;
    return obj;&lt;br /&gt;
  }&lt;br /&gt;
  public boolean isEmpty() { return size == 0; }&lt;br /&gt;
  public int size() { return size; }&lt;br /&gt;
  public int capacity() { return capacity; }&lt;br /&gt;
  public Object peek() {&lt;br /&gt;
    if (size == 0) return null;&lt;br /&gt;
    return q[consumerIndex];&lt;br /&gt;
  }&lt;br /&gt;
  public void clear() {&lt;br /&gt;
    Arrays.fill(q, null);&lt;br /&gt;
    size = 0;&lt;br /&gt;
    producerIndex = 0;&lt;br /&gt;
    consumerIndex = 0;&lt;br /&gt;
  }&lt;br /&gt;
  public Object clone() {&lt;br /&gt;
    return new CircularQueue(this);&lt;br /&gt;
  }&lt;br /&gt;
  public String toString() {&lt;br /&gt;
    StringBuffer s = new StringBuffer(super.toString() + &amp;quot; - capacity: &amp;quot;&amp;quot; +&lt;br /&gt;
      capacity() + &amp;quot;&amp;quot; size: &amp;quot;&amp;quot; + size() + &amp;quot;&amp;quot;&amp;quot;);&lt;br /&gt;
    if (size &amp;gt; 0) {&lt;br /&gt;
      s.append(&amp;quot; elements:&amp;quot;);&lt;br /&gt;
      for (int i = 0; i &amp;lt; size; ++i) {&lt;br /&gt;
        s.append(&amp;quot;\n&amp;quot;);&lt;br /&gt;
        s.append(&amp;quot;\t&amp;quot;);&lt;br /&gt;
        s.append(q[consumerIndex + i &amp;amp; bitmask].toString());&lt;br /&gt;
      }      &lt;br /&gt;
    }&lt;br /&gt;
    return s.toString();&lt;br /&gt;
  }&lt;br /&gt;
  public Iterator iterator() {&lt;br /&gt;
    return new Iterator() {&lt;br /&gt;
      private final int ci = consumerIndex;&lt;br /&gt;
      private final int pi = producerIndex;&lt;br /&gt;
      private int s = size;&lt;br /&gt;
      private int i = ci;&lt;br /&gt;
      public boolean hasNext() {&lt;br /&gt;
        checkForModification();&lt;br /&gt;
        return s &amp;gt; 0;&lt;br /&gt;
      }&lt;br /&gt;
      public Object next() {&lt;br /&gt;
        checkForModification();&lt;br /&gt;
        if (s == 0) throw new NoSuchElementException();&lt;br /&gt;
    &lt;br /&gt;
        s--;&lt;br /&gt;
        Object r = q[i];&lt;br /&gt;
        i = (i + 1) &amp;amp; bitmask;&lt;br /&gt;
        return r;&lt;br /&gt;
      }&lt;br /&gt;
      public void remove() {&lt;br /&gt;
        throw new UnsupportedOperationException();&lt;br /&gt;
      }&lt;br /&gt;
      private void checkForModification() {&lt;br /&gt;
        if (ci != consumerIndex) throw new ConcurrentModificationException();&lt;br /&gt;
        if (pi != producerIndex) throw new ConcurrentModificationException();&lt;br /&gt;
      }&lt;br /&gt;
    };&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Convert a Queue to a List ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.LinkedList;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
import java.util.Queue;&lt;br /&gt;
public class Main {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    Queue&amp;lt;String&amp;gt; myQueue = new LinkedList&amp;lt;String&amp;gt;();&lt;br /&gt;
    myQueue.add(&amp;quot;A&amp;quot;);&lt;br /&gt;
    myQueue.add(&amp;quot;B&amp;quot;);&lt;br /&gt;
    myQueue.add(&amp;quot;C&amp;quot;);&lt;br /&gt;
    myQueue.add(&amp;quot;D&amp;quot;);&lt;br /&gt;
    List&amp;lt;String&amp;gt; myList = new ArrayList&amp;lt;String&amp;gt;(myQueue);&lt;br /&gt;
    for (Object theFruit : myList)&lt;br /&gt;
      System.out.println(theFruit);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
/*&lt;br /&gt;
A&lt;br /&gt;
B&lt;br /&gt;
C&lt;br /&gt;
D&lt;br /&gt;
*/&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Create a queue using LinkedList class ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.util.LinkedList;&lt;br /&gt;
import java.util.Queue;&lt;br /&gt;
public class Main {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    Queue&amp;lt;String&amp;gt; queue = new LinkedList&amp;lt;String&amp;gt;();&lt;br /&gt;
    queue.offer(&amp;quot;First&amp;quot;);&lt;br /&gt;
    queue.offer(&amp;quot;Second&amp;quot;);&lt;br /&gt;
    queue.offer(&amp;quot;Third&amp;quot;);&lt;br /&gt;
    queue.offer(&amp;quot;Fourth&amp;quot;);&lt;br /&gt;
    System.out.println(&amp;quot;Size: &amp;quot; + queue.size());&lt;br /&gt;
    System.out.println(&amp;quot;Queue head using peek   : &amp;quot; + queue.peek());&lt;br /&gt;
    System.out.println(&amp;quot;Queue head using element: &amp;quot; + queue.element());&lt;br /&gt;
    Object data;&lt;br /&gt;
    while ((data = queue.poll()) != null) {&lt;br /&gt;
      System.out.println(data);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Synchronized Queue ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.&lt;br /&gt;
 * &lt;br /&gt;
 * Project: OpenSubsystems&lt;br /&gt;
 * &lt;br /&gt;
 * $Id: SynchronizedQueue.java,v 1.4 2007/01/07 06:14:00 bastafidli Exp $&lt;br /&gt;
 * &lt;br /&gt;
 * This program is free software; you can redistribute it and/or modify&lt;br /&gt;
 * it under the terms of the GNU General Public License as published by&lt;br /&gt;
 * the Free Software Foundation; version 2 of the License. &lt;br /&gt;
 * &lt;br /&gt;
 * This program is distributed in the hope that it will be useful,&lt;br /&gt;
 * but WITHOUT ANY WARRANTY; without even the implied warranty of&lt;br /&gt;
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the&lt;br /&gt;
 * GNU General Public License for more details.&lt;br /&gt;
 * &lt;br /&gt;
 * You should have received a copy of the GNU General Public License&lt;br /&gt;
 * along with this program; if not, write to the Free Software&lt;br /&gt;
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA &lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
import java.util.LinkedList;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
/**&lt;br /&gt;
 * Class that implement unlimited queue, that is synchronized. It means that&lt;br /&gt;
 * the consumer of the objects from the queue waits/is blocked in the get &lt;br /&gt;
 * method until there is an object available.&lt;br /&gt;
 *&lt;br /&gt;
 * @version $Id: SynchronizedQueue.java,v 1.4 2007/01/07 06:14:00 bastafidli Exp $&lt;br /&gt;
 * @author Miro Halas&lt;br /&gt;
 * @code.reviewer Miro Halas&lt;br /&gt;
 * @code.reviewed Initial revision&lt;br /&gt;
 */&lt;br /&gt;
public class SynchronizedQueue&lt;br /&gt;
{&lt;br /&gt;
   // Attributes ///////////////////////////////////////////////////////////////&lt;br /&gt;
   &lt;br /&gt;
   /**&lt;br /&gt;
    * Cache of object produced by producer and consumed by consumer.&lt;br /&gt;
    */&lt;br /&gt;
   protected List m_lstObjects;&lt;br /&gt;
   // Constructors /////////////////////////////////////////////////////////////&lt;br /&gt;
   &lt;br /&gt;
   /**&lt;br /&gt;
    * Constructor for Synchronized Queue Object.&lt;br /&gt;
    */&lt;br /&gt;
   public SynchronizedQueue(&lt;br /&gt;
   )&lt;br /&gt;
   {&lt;br /&gt;
      super();&lt;br /&gt;
      m_lstObjects = new LinkedList();&lt;br /&gt;
   }&lt;br /&gt;
   // Logic ////////////////////////////////////////////////////////////////////&lt;br /&gt;
   &lt;br /&gt;
   /**&lt;br /&gt;
    * Destructor for Synchronized Queue. It is called when no other&lt;br /&gt;
    * object holds reference to it.&lt;br /&gt;
    *&lt;br /&gt;
    * @exception Throwable - default destructor exception&lt;br /&gt;
    */&lt;br /&gt;
   protected void finalize(&lt;br /&gt;
   ) throws Throwable&lt;br /&gt;
   {&lt;br /&gt;
      // Explicitely remove this just to help garbage collector&lt;br /&gt;
      m_lstObjects.clear();&lt;br /&gt;
      m_lstObjects = null;&lt;br /&gt;
      super.finalize();&lt;br /&gt;
   }&lt;br /&gt;
   /**&lt;br /&gt;
    * Get the object from the beginning of the queue&lt;br /&gt;
    *&lt;br /&gt;
    * @return Object - object from the queue, if the thread is blocked in this&lt;br /&gt;
    *                  function and you call interrupt method, an InterruptedException&lt;br /&gt;
    *                  will be thrown.&lt;br /&gt;
    * @exception InterruptedException - if the thread is blocked in this&lt;br /&gt;
    *                                   function and you call interrupt method,&lt;br /&gt;
    *                                   an InterruptedException will be thrown.&lt;br /&gt;
    */&lt;br /&gt;
   public synchronized Object get(&lt;br /&gt;
   ) throws InterruptedException&lt;br /&gt;
   {&lt;br /&gt;
      Object objReturn = null;&lt;br /&gt;
      if (m_lstObjects.isEmpty())&lt;br /&gt;
      {&lt;br /&gt;
         // There is no object in the queue, go to sleep&lt;br /&gt;
         try&lt;br /&gt;
         {&lt;br /&gt;
            wait();&lt;br /&gt;
         }&lt;br /&gt;
         catch (InterruptedException ieException)&lt;br /&gt;
         {&lt;br /&gt;
            // Somebody woke us up, that means all threads waiting on this&lt;br /&gt;
            // object competed for the lock and this one won and the object is&lt;br /&gt;
            // locked again&lt;br /&gt;
            // The thread can be woken up in two conditions, producer put new&lt;br /&gt;
            // object into the queue or somebody called interrupt - to interrupt&lt;br /&gt;
            // the wait - in this case rethrow an exception&lt;br /&gt;
            if (m_lstObjects.isEmpty())&lt;br /&gt;
            {&lt;br /&gt;
               throw ieException;&lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
      // Remove the first object in the queue&lt;br /&gt;
      objReturn = m_lstObjects.remove(0);&lt;br /&gt;
      return objReturn;&lt;br /&gt;
   }&lt;br /&gt;
   /**&lt;br /&gt;
    * Put the object to the end of the queue.&lt;br /&gt;
    *&lt;br /&gt;
    * @param objNew - new object, can be null&lt;br /&gt;
    */&lt;br /&gt;
   public synchronized void put(&lt;br /&gt;
      Object objNew&lt;br /&gt;
   )&lt;br /&gt;
   {&lt;br /&gt;
      m_lstObjects.add(objNew);&lt;br /&gt;
      // New object in the queue, notify others&lt;br /&gt;
      notifyAll();&lt;br /&gt;
   }&lt;br /&gt;
   /**&lt;br /&gt;
    * Test if the queue is empty.&lt;br /&gt;
    *&lt;br /&gt;
    * @return boolean - true if the queue is empty&lt;br /&gt;
    */&lt;br /&gt;
   public synchronized boolean isEmpty(&lt;br /&gt;
   )&lt;br /&gt;
   {&lt;br /&gt;
      return m_lstObjects.isEmpty();&lt;br /&gt;
   }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Use the Stack class in Java ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.util.Stack;&lt;br /&gt;
public class Main {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    Stack&amp;lt;Integer&amp;gt; stack = new Stack&amp;lt;Integer&amp;gt;();&lt;br /&gt;
    for (int i = 0; i &amp;lt; 10; i++) {&lt;br /&gt;
      stack.push(i);&lt;br /&gt;
      System.out.print(i + &amp;quot; &amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    System.out.println(&amp;quot;&amp;quot;);&lt;br /&gt;
    int position = stack.search(3);&lt;br /&gt;
    System.out.println(&amp;quot;Search result position: &amp;quot; + position);&lt;br /&gt;
    System.out.println(&amp;quot;Stack top: &amp;quot; + stack.peek());&lt;br /&gt;
    while (!stack.empty()) {&lt;br /&gt;
      System.out.print(stack.pop() + &amp;quot; &amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;/div&gt;</summary>
			</entry>

	</feed>