B
Bill David
In my application, I will iterate a map and send out a message to
another class with the key of current object to which iterator is
pointing. And the operation is in the same thread. This class get the
notification will do more handle and then, it will try to remove this
object from the map with the key.
But when I try to go on to the next object in the map, I will get the
ConcurrentModificationException.
I know if I call Iterator.remove during iterating, no exception will
happen. But I can't do that, as I must ensure the other class has
completely handled the notification.
// OK, but not what I want
Map<Integer, String> sessions = plugin.getSessions();
Set<Map.Entry<Integer, String>> set = sessions.entrySet();
for (Iterator<Map.Entry<Integer, String>> it = set.iterator();
it.hasNext() {
// send out notification...
it.remove();
}
// Failed
Map<Integer, String> sessions = plugin.getSessions();
Set<Map.Entry<Integer, String>> set = sessions.entrySet();
for (Iterator<Map.Entry<Integer, String>> it = set.iterator();
it.hasNext() {
// send out notification...
}
// in another class, after check and handle the notification. If
failed, should not release resource.
void release(int key) {
Map<Integer, String> sessions =
((MockManagedPlugin)plugin).getSessions();
sessions.remove(key);
}
Do you have any suggestion to me about this issue?
another class with the key of current object to which iterator is
pointing. And the operation is in the same thread. This class get the
notification will do more handle and then, it will try to remove this
object from the map with the key.
But when I try to go on to the next object in the map, I will get the
ConcurrentModificationException.
I know if I call Iterator.remove during iterating, no exception will
happen. But I can't do that, as I must ensure the other class has
completely handled the notification.
// OK, but not what I want
Map<Integer, String> sessions = plugin.getSessions();
Set<Map.Entry<Integer, String>> set = sessions.entrySet();
for (Iterator<Map.Entry<Integer, String>> it = set.iterator();
it.hasNext() {
// send out notification...
it.remove();
}
// Failed
Map<Integer, String> sessions = plugin.getSessions();
Set<Map.Entry<Integer, String>> set = sessions.entrySet();
for (Iterator<Map.Entry<Integer, String>> it = set.iterator();
it.hasNext() {
// send out notification...
}
// in another class, after check and handle the notification. If
failed, should not release resource.
void release(int key) {
Map<Integer, String> sessions =
((MockManagedPlugin)plugin).getSessions();
sessions.remove(key);
}
Do you have any suggestion to me about this issue?