tags:

views:

66

answers:

4

Hi,

I am completely new to java, but I have urgent requirement to create a queue and thread. I am confused which queue class must be used.

Here's the scenario:

I need to a thread to handle user events from the application layer as well as callback events from the lower middleware layer. For this purpose, it was decided that a queue will be maintained. Events will be posted to this queue whenever a user event or callback event occurs. The thread polls for events in the queue and takes appropriate action. The same queue can be written into by different classes(i.e application layer & lower layer). Hence, which queue wuld be safer, to ensure the same location is not being written into simultaneously by different classes?

Also, what is the basic one-sentence difference between a Queue, BlockingQueue and ArrayBlockingQueue and in what scenarios must each be selected?

Regards, kiki

+1  A: 

Of the three you listed, the only which is actually a class is ArrayBlockingQueue. A blocking queue is different from a normal queue in that, if an object attempts to remove the front item, it will pause execution until there is an available item to remove.

"BlockingQueue" and "Queue" are just a interfaces; you can't instantiate them. Types of BlockingQueue that you can instantiate are ArrayBlockingQueue, LinkedBlockingQueue, etc.

Personally, I would use a LinkedBlockingQueue for this application - the advantage of using a linked list is that there's no set max capacity, and the memory usage decreases as the queue shrinks.

Sam Dufel
+3  A: 

In connection to "few words difference": Queue and BlockingQueue are interfaces, whereas ArrayBlockingQueue is a class which imiplements BlockingQueue interface.

You should choice mainly between ConcurrentLinkedQueue and ArrayBlockingQueue/LinkedBlockingQueue.

Former gives you unbounded queue ( not limite sin size), latter provide fixed-size queues which wait for space to become available in the queue when storing an element.

As an alternative to queues + threads you can consider Executor and Future interfaces from concurrent package, they may be easier in usage to implement client-server model.

maxim_ge
LinkedBlockingQueue isn't limited in size - it's a linked list implementation
Sam Dufel
It is and the size is specified by constructor, well, it can be Integer.MAX_VALUE :)
maxim_ge
+1  A: 

Use the higher-level Executors.newSingleThreadExecutor()

CurtainDog
I think you mean Executors.newSingleThreadExecutor()
NamshubWriter
@NamshubWriter - indeed I do, thanks for that
CurtainDog
+2  A: 

For your scenario, what you need is a thread safe queue such as ConcurrentLinkedQueue. Regarding your other question on Queue and BlockingQueue. There are basically the following types of queue implementations:

Blocking: Blocks until the operation (put(),take() etc.) is possible with an optional timeout. Non-Blocking: The operation completes instantly

Bound: Has a upper limit on the number of items in the queue

Non-bound: No limit on the number of items in the queue.

As for ArrayBlockingQueue, it is backed up by an Array while a LinkedBlockingQueue is backed up by a LinkedList.

Suresh Kumar