This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Session Counter Locking Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Session Counter Locking Question" Watch "Session Counter Locking Question" New topic

Session Counter Locking Question

A Phatak

Joined: Sep 22, 2011
Posts: 24
I keep having some trouble understanding synchronizing concept in the context of static vs nonstatic. For e.g. take a look at the code below:

It is a simple listener that counts number of active sessions. However, we know HttpSession object is not thread safe. So I believe that "counter" variable should be incremented in a "synchronized" manner.

It is clear that getCounter() should change to public synchronized static int getSessionCounter()

My question is what changes should be implemented in sessionDestroyed() and sessionCreated() methods? Which of the following is correct?


Or does it have to be synchronized at all?

ocpjp 6 (86%)
I agree. Here's the link:
subject: Session Counter Locking Question
Similar Threads
doubt with the answer
how would the sessionListener object be accessed
Synchronizing Session Counter
HttpSessionListener : Where can i see this output ?
Updating a JSP session attribute and html code on mouse click