我有一个ConcurrentHashmap,它被多个线程更新。
ConcurrentHashMap<String, BlockingQueue<StateObject>> querystatestore = new ConcurrentHashMap<String, BlockingQueue<StateObject>>();
当线程之一尝试从某个阻塞队列中提取对象并尝试处理该对象时,它会在迭代后抛出 java.util.ConcurrentModificationException 。 从Blockingqueue读取的代码段是
public class ExecuteMatcher implements Runnable {
public BlockingQueue<StateObject> state;
public String queryname;
/**
* @param state
* @param queryname
*/
public ExecuteMatcher(BlockingQueue<StateObject> state, String queryname) {
super();
this.state = state;
this.queryname = queryname;
}
public void executematcher(BlockingQueue<StateObject> state1, String queryname2) {
// need to add poison pill else the loop willl never end....
while (true) {
StateObject object;
try {
object = state1.take();
if (object != null) {
// Thread.sleep(100);
printstate(object);
// Thread.sleep(100);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void printstate(StateObject state1) {
for (PublicationGraphEvent ge : state1.getState()) {
for (VertexObject vertex : ge.getPubGraph().vertexSet()) {
System.out.println("When STate fetehed from blocking queue: " + vertex.getVertex_label()
+ " Vertex_id: " + vertex.getVertex_id() + "Eventid: " + ge.getEventID());
}
}
}
/*
* (non-Javadoc)
*
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
// TODO Auto-generated method stub
this.executematcher(state, queryname);
}
}
这是在for循环中发生的,而迭代是 for(PublicationGraphEvent ge:state1.getState())的。我不明白为什么我的清单会被更新。有什么方法可以停止 state1.take(); 功能,直到本方法完成,以便我可以从队列中取出另一个对象来处理它。在这一点上,我真的很挣扎,任何帮助将不胜感激。