更新存储在阻塞优先级队列中的自定义对象
我有一个阻塞优先级队列,它存储消息类型的对象,消息有 String[] data = new String[10]。现在我必须迭代整个阻塞队列,检查其对象消息的第二个元素是否等于传入消息的第六个元素。
Messages 的比较器不是基于需要更新的第 6 个元素。问题是,如果我取出一个对象,那么如何将它放在相同的位置,如果我使用下面的代码来更新它,那么每当运行 iter.next() 时,它可能会开始指向下一个对象。
这就是我正在尝试的。
public synchronized void updateAck(Message ackMessage)
{
Iterator iter = localQ.iterator(); // localQ is the blocking priority queue here
while(iter.hasNext())
{
if(((Message)iter.next()).data[2].equalsIgnoreCase(ackMessage.data[6]))
{
(Integer.parseInt((Message)iter.next()).data[6])+1);
}
}
}
I have a blocking priority queue which stores Objects of type Message, message has String[] data = new String[10]. Now I have to iterate over whole blocking Queue, check if its Object message's 2nd element is equal to 6th element of an incoming message.
The comparator of Messages is not based on 6th element which needs to be updated. Problem is that if I take out an object then how to put it at same position and if I use the code below to update it then anytime iter.next() is run it may start pointing to next Object.
Here is what I am trying.
public synchronized void updateAck(Message ackMessage)
{
Iterator iter = localQ.iterator(); // localQ is the blocking priority queue here
while(iter.hasNext())
{
if(((Message)iter.next()).data[2].equalsIgnoreCase(ackMessage.data[6]))
{
(Integer.parseInt((Message)iter.next()).data[6])+1);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要在
if
条件中直接使用(Message)iter.next()
,而是尝试这样做。完整代码
Instead of directly using
(Message)iter.next()
in yourif
conditions, try this.Full code
似乎在实时队列上执行此操作有些危险,具体取决于下游消费者的期望。
怎么样:
如果这种情况经常发生,也许您需要一对队列;一个是活的;另一个是影子。
It seems like doing this operation on a live queue is somewhat dangerous, depending on what the downstream consumer is expecting.
How about this:
BlockingQueue
If this happens a lot, maybe you need a pair of queues; one is the live one; the other is the shadow.