aspose file tools*
The moose likes Servlets and the fly likes Listeners automatically thread safe? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Listeners automatically thread safe?" Watch "Listeners automatically thread safe?" New topic
Author

Listeners automatically thread safe?

Jay Howard
Greenhorn

Joined: Mar 01, 2005
Posts: 6
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.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
It is taken care by the container, as we make an entry of listener in our web.xml file.
It wouldn't give you bogus result.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If you're just incrementing or decrementing the value of the instance variable, you won't have any threading issues. If, however, you want to actually do something with that variable, you should synchronize access to it so another thread doesn't change it in between the time that this one changes it and reads it.


[ March 02, 2005: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Yeah quite right Ben. Like I did some DB operations on sessionDestoryed and sessionCreated events, I made that synchronized by myself.
Jay Howard
Greenhorn

Joined: Mar 01, 2005
Posts: 6
If you're just incrementing or decrementing the value of the instance variable, you won't have any threading issues.

This followup question has nothing to do with servlets, but is the above really correct? I thought since "foo++" was equivalent to "foo = foo + 1" that there could still be problems:

Thread A: Computes temporary value "foo + 1"
Thread B: Computes temporary value "foo + 1"
Thread A: Stores temporary value "foo + 1" in "foo"
Thread B: Stores temporary value "foo + 1" in "foo"

The result being foo only gets incremented once.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Hmm,

I'll have to plead ignorance on this one.
I don't know if the '++' and '--' operators are synchronized or not.
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Henry Wong on another topic said that the ++ operator is "is syntactic sugar for a load, increment, and store. It is *not* atomic."


James Carman, President<br />Carman Consulting, Inc.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Listeners automatically thread safe?