简体   繁体   中英

[Java]: Which kind of queue to use for my scenario?

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(ie 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

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.

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.

使用更高级别的Executors.newSingleThreadExecutor()

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM