I am trying to make a servlet thread-safe with sessions. I have read up on different techniques such as a synchronized block, AtomicReference or ConcurrentHashMap. What are the trade-offs for each technique, if any?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The first aim for a servlet is to achieve thread safety by virtue of no shared state. Any shared state will fail to be that when the servlet is deployed into a load-balancing cluster. So if your shared state is not of a cache flavor, meaning it can always be rebuilt from a durable store, you shouldn’t have it in the first place.
But, apart from these concerns, you cannot get a one-size-fits-all answer without giving any details on the problem you are trying to solve with shared state. All the techniques you mention have merit, that’s why they are still around with us after 15 years of experience with Java.