Interview Questions

Java Program - ThreadSafe Circular Queue

The following is a basic thread safe implementation of a circular queue of user-specified size using a simple array. It contains routines to initialize(), enqueue() and dequeue() the queue.

/**
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */


package com.tdd;

/**
 * A basic threadSafe Circular Queue implementation
 * with enqueue(),dequeue() operations. Queue size shoule be initialized by the client.
 */

public class CircularQueue {

        private int qSize = 0;
        private Integer[] cQArray = new Integer[20];
        private int firstPointer = -1;
        private int lastPointer = -1;
        private int count = 0;

        public CircularQueue() {
               
          // TODO Auto-generated constructor stub

        }

        public synchronized boolean isEmpty() {
                if (count == 0) {

                        return true;
                }
                return false;
        }

        public synchronized boolean isFull() {
                if (count == qSize) {

                        return true;
                }
                return false;
        }

        public synchronized void initialize(int qSize) {
                cQArray = new Integer[qSize];
                this.qSize = qSize;
                firstPointer = -1;
                lastPointer = -1;
        }

        public synchronized void enqueue(int item) {
                System.out.println("enqueue->");

                if (isFull())
                        System.out.println("queue is full");
                else {
                        count++;
                        if (lastPointer + 1 < qSize) {
                                cQArray[lastPointer + 1] = item;
                                lastPointer++;

                        } else {
                                lastPointer = -1;
                                cQArray[lastPointer + 1] = item;
                                lastPointer++;
                        }

                        if (firstPointer == -1)
                                firstPointer++;
                }
                System.out.println("lastPointer->" + lastPointer);
                System.out.println("firstPointer->" + firstPointer);
        }

        public synchronized void dequeue() {
                System.out.println("dequeue->");

                if (isEmpty()) {
                        System.out.println("queue is empty");
                        initialize(cQArray.length);
                        return;
                }
                Integer item = cQArray[firstPointer];
                System.out.println("item removed from queue is " + item);
                count--;

                cQArray[firstPointer] = 0;
                if (firstPointer + 1 == qSize)
                        firstPointer = 0;
                else
                        firstPointer++;

                System.out.println("lastPointer->" + lastPointer);
                System.out.println("firstPointer->" + firstPointer);
        }

        public void printQueue() {
       
                System.out.println("queue is -> ");
                for (int i = firstPointer; i < cQArray.length; i++) {
                        if (firstPointer >= 0)
                                System.out.print(cQArray[i] + ",");
                }

                for (int i = 0; i < firstPointer; i++) {
                        if (firstPointer >= 0)
                                System.out.print(cQArray[i] + ",");
                }
        }
}