This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Listeners automatically thread safe?

 
Jay Howard
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2874
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah quite right Ben. Like I did some DB operations on sessionDestoryed and sessionCreated events, I made that synchronized by myself.
 
Jay Howard
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong on another topic said that the ++ operator is "is syntactic sugar for a load, increment, and store. It is *not* atomic."
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic