My program requires me to take data from two queues and sort them into a priority queue. The first queue is for planes landing and it takes priority over the second queue of planes trying to take off. I am having trouble understanding how to set up the priority queue and take the two separate queues and sort them correctly into the priority queue.
A priority queue should be automatically orderered.
That is to say, when you poll it takes the least element according to the specified ordering (or natural ordering if none is specified).
So if you want to use a priority queue, write a comparator so that the elements from the first queue get selected first (perhaps using a wrapper class).
A better solution might be to write your own queue. When polling from this queue, just check the first queue for available items, if none are available, check the second.
Related
I have two definitions from some college notes I'm reading.
"dequeue(): Remove the object from the front of the queue and return it; an error occurs if the queue is empty"
"front(): Return the front object in the queue, BUT DO NOT remove it; an error occurs if the queue is empty"
I understand the dequeue method but the front method has me a bit perplexed. Just wondering if someone has a good example of the front method being used so I can get my head around the difference between the two. Thanks.
Imagine a scenario with a single producer and multiple consumers. A particular thread-safe queue is used as the buffer between the different producers and consumers.
Now imagine that a particular consumer only has the ability to process a certain type of data from the queue. It could use the front() method to peek at the next data to see if it actually can process it and then call dequeue() if it can. If it cannot, it simply won't call dequeue(), leaving the queue unmodified.
Arguably, in the same scenario, you could call dequeue() to obtain the data, examine it, determine if you can process it. If not, add to the front of the queue again. But in this, it takes a lot more effort as the queue is being modified twice, and the action of putting an element back at the front of the queue may be expensive or completely prohibited. Most likely, you're adding the element to the end of the queue, disrupting the queue process order.
The front() method is a method to optimize queue access by keeping the number of modifications being done to the queue to a minimum given that queue modifications are usually more expensive than simple peeks at it. By looking at the first element without accessing it, consumers can decide if they are actually going to modify the queue, reducing the number of modifications when compared to dequeue() and the re-queuing the data.
Assume you have a Queue of Integers that you are iterating over for whatever reason. You have a label on your UI that shows the upcoming number. To update that label, you will use youeQueue.front() to retrieve the number in question without removing it. When your next calculation starts, your calculation method will use yourQueue.dequeu() to retrieve the next element and remove it from the queue.
I need a blocking queue that has a size of 1, and every time put is applied it removes the last value and adds the next one. The consumers would be a thread pool in which each thread needs to read the message as it gets put on the queue and decide what to do with it, but they shouldn't be able to take from the queue since all of them need to read from it.
I was considering just taking and putting every time the producer sends out a new message, but having only peek in the run method of the consumers will result in them constantly peeking, won't it? Ideally the message will disappear as soon as the peeking stops, but I don't want to use a timed poll as it's not guaranteed that every consumer will peek the message in time.
My other option at the moment is to iterate over the collection of consumers and call a public method on them with the message, but I really don't want to do that since the system relies on real time updates, and a large collection will take a while to iterate through completely if I'm going through each method call on the stack.
After some consideration, I think you're best off, with each consumer having its own queue and the producer putting its messages on all queues.
If there are few consumers, then putting the messages on those few queues will not take too long (except when the producer blocks because a consumer can't keep up).
If there are many consumers this situation will be highly preferable over a situation where many consumers are in contention with each other.
At the very least this would be a good measure to compare alternate solutions against.
Is there a way to iterate a LinkedBlockingQueue starting with a specific index number?
I have a LinkedBlockingQueue that contains a list of changes to make to a game world. It works perfectly fine when I'm actually making those changes, which involves iterating the queue, using the object polled this iteration, and then removing it from the queue.
The next time the process runs, iterating the queue from the beginning works because we had removed all "used" items.
However, I also have a preview mode, where the changes from the queue need to read and shown to the player, but not actually removed from the queue yet (since they're not officially "used")
These are all done in batches of 1000 so we don't overload network traffic or the clients.
I'd rather not have to re-iterate the queue each "batch" and use something to tell us to continue on until a specific index - and I'd rather not create a secondary queue or "holder".
I use a PriorityBlockingQueue to maintain a list of objects whose order is dictated using a comparator. My requirement is as follows: First, I add N objects to the queue, and the queue maintains the ordered list with it. Later, I change the value in the objects that had been added to the queue. The issue is that the queue is not refreshed based on the updated values in the objects. In contrast, I observed that the queue is refreshed when a single object is removed.
Is there anyway I can manually refresh the values in the queue before obtaining values from it in a very efficient manner?
Not with PriorityBlockingQueue. It sounds like the option you're looking for is decrease-key, which isn't supported by the Java priority queue abstractions.
I want to create a blocking queue which blocks producer on the basis of customized rules instead of number of items in the queue.
For example:
Producer produces some files and puts into a queue. Consumer transfers them to a specific location after some analysis.
For above scenario, I want producer waiting to produce new files if the size of total files in the queue reaches some threshold value. Queue can accept any number of files if the total size don't cross threshold value.
I would probably subclass a BlockingQueue such as the ArrayBlockingQueue and add a simple CountDownLatch which is initialized to the threshold value and enables the various take/remove methods when reaching 0.
I think you will have to implement this locking mechanism yourself. You could use wait/notify or ReentrantLock/Condition, a long variable holding the combined length and a LinkedList holding the files.