What if you want to implement your own Queue class in Java? The Queue module provides a FIFO implementation suitable for multi-threaded programming. It can be used to pass messages or other data between producer and consumer threads safely.
Look at this scenario:
- Create a global Singleton Queue object through out your project.
- One Process of Object may use this Singleton object to add String at any given time.
- At the same time another Process or Object use the same Singleton Object to get what ever you pushed 1st in FIFO order. Basically implement a Queue Object – A thread-safe FIFO implementation.
Well, I had the same requirement last week for my project. So I’ve created one simple Queue Object in Java which solved my above issue. Wanted to share the same with my readers..
Here is a Java Project:
Create class CrunchifyQueueInJava.java
and CrunchifyQueueTest.java
package crunchify.com.tutorial; import java.util.LinkedList; import java.util.Queue; /** * @author Crunchify.com * Simple Queue implementation in Java */ public class CrunchifyQueueInJava { static Queue<String> queue = new LinkedList<String>(); private static CrunchifyQueueInJava queueInstance = null; public static CrunchifyQueueInJava getStreamInstance() { if (queueInstance == null) { queueInstance = new CrunchifyQueueInJava(); } return queueInstance; } public Queue<String> get() { return queue; } // Inserts the specified element into this queue if it is possible to do so // immediately without violating capacity restrictions public void add(String value) { synchronized (queue) { queue.add(value); } } // Removes a single instance of the specified element from this collection public void remove(String value) { synchronized (queue) { queue.remove(value); } } // Retrieves and removes the head of this queue, or returns null if this // queue is empty. public String poll() { String data = queue.poll(); return data; } // Returns true if this collection contains no elements public boolean isEmpty() { return queue.isEmpty(); } // Returns the number of elements in this collection. If this collection // contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE public int getTotalSize() { return queue.size(); } }
Another must read: Simple Linked-list implementation in Java
Now let’s create sample Test Class:
package crunchify.com.tutorial; /** * @author Crunchify.com * */ public class CrunchifyQueueTest { public static void main(String args[]) { CrunchifyQueueInJava crunchifyQueue = CrunchifyQueueInJava.getStreamInstance(); crunchifyQueue.add("Crunchify"); crunchifyQueue.add("Google"); crunchifyQueue.add("Yahoo"); crunchifyQueue.add("Facebook"); System.out.println("Initial Queue: " + crunchifyQueue.get()); crunchifyQueue.remove("Google"); System.out.println("After removing Google: " + crunchifyQueue.get()); System.out.println("totalSize: " + crunchifyQueue.getTotalSize()); System.out.println("isEmpty(): " + crunchifyQueue.isEmpty()); System.out.println("poll(): " + crunchifyQueue.poll()); System.out.println("After Polling: " + crunchifyQueue.get()); } }
Result:
Initial Queue: [Crunchify, Google, Yahoo, Facebook] After removing Google: [Crunchify, Yahoo, Facebook] totalSize: 3 isEmpty(): false poll(): Crunchify After Polling: [Yahoo, Facebook]