I'm currently going through Headfirst
JSP and encountered this example code for a HttpSessionListener (p259):
public class BeerSessionCounter implements HttpSessionListener
{
static private int activeSessions;
public static int getActiveSessions() { return activeSessions; }
public void sessionCreated(HttpSessionEvent event) { activeSessions++; }
public void sessionDestroyed(HttpSessionEvent event) { activeSessions--; }
}
Is this safe because all Listeners execute serially in their own
thread, or are the ++ and -- operators atomic? If not, it seems like activeSessions could wind up with an incorrect value if a session is created at the same time another one is destroyed.