• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Listeners automatically thread safe?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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."
 
Bras cause cancer. And tiny ads:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic