I currently have a parallel queue implementation that uses BlockingQueue as a data store. Now I need to enter an object of the second type with a higher priority, which will lead me to the hunger / priority queue for the original queue. Therefore, we work with objects of type A and type B, which are produced from several threads. Any type B objects must be processed to those of type A, but this FIFO order is not supported. Therefore, if {1A, 1B, 2A, 3A, 2B} is inserted, the order should be {1B, 2B, 1A, 2A, 3A}
I tried one PriorityBlockingQueue to push type B to the front, but I could not support the FIFO requirement (there is no natural order between elements of the same type).
My next thought is to use two parallel queues. I am looking for common errors or considerations when coordinating access between two queues. Ideally, I would like to do something like this:
public void add(A a) { aQueue.add(a); } public void add(B b) { bQueue.add(b); } private void consume() { if(!bQueue.isEmpty()) process(bQueue.poll()); else if(!aQueue.isEmpty()) process(aQueue.poll()); }
Do I need any synchronization or blocking if both queues are ConcurrentLinkedQueue (or insert a more appropriate structure here)? Note. I have many manufacturers, but only one consumer (single-threaded ThreadPoolExecutor ).
EDIT: If after checking isEmpty () B appears, normally process A and process it the next time consumption () is called.
source share