I implemented a producer-consumer pattern with Java’s LinkedBlockingDeque, but I have hit a problem, that I sometimes want to move an item (which is alread somewhere in the queue) to the front of the queue, so it is processed sooner. I never know which of the already enqueued items would be good to move to the front. So I would like the LinkedBlockingDeque to be replaced by something, where I can reorder items. I have one producer and 2-4 consumers (custom Thread implementation) and I am on Android platform. How can I do this? Some blocking double connected linked list?
I implemented a producer-consumer pattern with Java’s LinkedBlockingDeque, but I have hit a problem,
Share
From my comment:
You could use
remove(Object o)to retrieve the object and remove it from the queue, then useaddFirst(Object o)to add it to the front of the queue. This will have the effect of prioritising this object.You use the iterator to check what object are inside the Deque. However be very careful of
ConcurrentModificationExceptionas they could be raised if you are accessing the Collection in one thread and modifying it in another, however if you are using a blockingQueue this shouldn’t be an issue.From BlockingQueue Javadoc: