Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8651997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:12:17+00:00 2026-06-12T14:12:17+00:00

// in a context listener public void contextInitialized(ServletContextEvent sce) { SessionListener.context = sce.getServletContext(); HashMap<String,

  • 0
// in a context listener
public void contextInitialized(ServletContextEvent sce) {
        SessionListener.context = sce.getServletContext();
        HashMap<String, String> messages = new HashMap<>();
        context.setAttribute("messages", messages);
}

Now I want to access the messages map from various servlets – what about synchronization ?

Namely I want to add an element to the map (whose key must be unique) – so I have to try a couple of times maybe – except if there is some method in the java ee api (?)

EDIT : interested also in synchronizing access to a session scoped map

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:12:18+00:00Added an answer on June 12, 2026 at 2:12 pm

    This map is a non-thread-safe object shared by multiple threads. So every access to the map should be synchronized. You have various options:

    • have each client of the map synchronize on the map itself: very fragile
    • use a thread-safe map like Collections.synchronizedMap() or ConcurrentHashMap. This will never let the map in an inconsistent state, but additional synchronization could still be needed for operations that should be atomic but involve several method calls on the map
    • encapsulate the map into an object of your own, and make sure that this object provides all the necessary operations that need to be done on the map. These operations should of course perform the necessary synchronization.

    The third solution is probably the best one. The second one might be OK if the operations on the map are very simple, and coveredby methods of the map.

    Regarding the generation of unique and random strings, you could combine a UUID (for uniqueness) and a Random (or SecureRandom, depending on your requirements), for the randomness.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I start h2 database in a servlet context listener: public void contextInitialized(ServletContextEvent sce) {
I have this piece of code: public static DateDialogFragment newInstance(Context context, DateDialogFragmentListener listener) {
I have a listener interface: public interface AnimationListener { public void onAnimationStarted(Animation animation); public
public class ExampleAppWidgetProvider extends AppWidgetProvider { @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[]
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); db = new DbAdapter(getBaseContext()); db.open(); android_id =
I need the context to ApplicationContext.xml ,which I provided in web.xml as <listener> <listener-class>
Context Clojure Agents are NOT sent new values. They are sent a function which
i have code like this. this is on create public void onCreate(Bundle savedInstanceState) {
public class ReplyWithAddressService extends Service{ public static final String GOOGLE_GEOCODER = http://maps.googleapis.com/maps/api/geocode/json?latlng=; private String
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); LocationListener mlocListener = new MyLocationListener(); mlocManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.