Thanks to anyone who can help me here - I'm very confused.
I have a class WebAppUtility that looks like this:
private static Set<HttpSession> openSessions = Collections
.synchronizedSet(new HashSet<HttpSession>());
public static void addSession(HttpSession session) {
synchronized(openSessions){
openSessions.add(session);
}
}
public static boolean removeSession(HttpSession session) {
synchronized(openSessions){
return openSessions.remove(session);
}
}
public static Set<String> getLoggedInUsers() throws Exception {
Set<String> set = new HashSet<String>();
synchronized (openSessions) {
for(Iterator i = openSessions.iterator(); i.hasNext();){
HttpSession session = (HttpSession)i.next();
//grab attribute from session here and stick in "set"
}
}
return set;
}
Somehow when I have many users logging in and out I'm getting a ConcurrentModificationException under the getLoggedInUsers method:
java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:787)
at java.util.HashMap$KeyIterator.next(HashMap.java:823)
at com.studentuniverse.utility.WebAppUtility.getLoggedInUsers(WebAppUtility.java:68)
Line 68 is the one that looks like:
HttpSession session = (HttpSession)i.next();
Am I misusing the synchronizedSet in some way? Please guide me to proper threaded Set usage! Thank you very much.
_kris