This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread Safe Sessions  RSS feed

 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you implement the methods of the HttpSessionListener interface, is it always a good idea to ensure that the code is thread safe? I'm thinking, for instance, that if you were to remove a session from a Collection in the sessionDestroyed() method, it's possible that two or more threads may be running that method at the same time.

I presume that if you had to write thread safe code, then you should synchronize the code which accesses the Collection on the HttpSession object (as I won't have thought that the entire method needs to be synchronized).
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I think its needed, make it thread-safe. Not the entire method but just the code accessing the collection.

If you are using ArrayList it means you have to make it threadsafe by yourself. However, unlike ArrayList, Vector is sychronized.

More inputs are welcome.

Thanks
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!