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 8286529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:48:00+00:00 2026-06-08T11:48:00+00:00

I am referring to question asked here and using authors code example, now my

  • 0

I am referring to question asked here and using authors code example, now my question is

  1. Why does author uses synchronized(synchronizedMap), is it really necessary because synchronizedMap will always make sure that there are no two threads trying to do read/put operation on Map so why do we need to synchronize on that map itself?

Would really appreciate an explanation.


  public class MyClass {
  private static Map<String, List<String>> synchronizedMap =
      Collections.synchronizedMap(new HashMap<String, List<String>>());

  public void doWork(String key) {
    List<String> values = null;
    while ((values = synchronizedMap.remove(key)) != null) {
      //do something with values
    }
  }

  public static void addToMap(String key, String value) {
    synchronized (synchronizedMap) {
      if (synchronizedMap.containsKey(key)) {
        synchronizedMap.get(key).add(value);
      }
      else {
        List<String> valuesList = new ArrayList<String>();
        valuesList.add(value);
        synchronizedMap.put(key, valuesList);
      }
    }
  }
}
  • 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-08T11:48:02+00:00Added an answer on June 8, 2026 at 11:48 am

    why do we need to synchronize on that synchronizemap itself?

    You may need to synchronize on an already synchronized collection because you are performing two operations on the collection — in your example, a containsKey() and then a put(). You are trying to protect against race conditions in the code that is calling the collection. In addition, in this case, the synchronized block also protects the ArrayList values so that multiple threads can add their values to these unsynchronized collections.

    If you look at the code you linked to, they first check for the existence of the key and then put a value into the map if the key did not exist. You need to protect against 2 threads checking for a key’s existence and then both of them putting into the map. The race is which one will put first and which one will overwrite the previous put.

    The synchronized collection protects itself from multiple threads corrupting the map itself. It does not protect against logic race conditions around multiple calls to the map.

    synchronized (synchronizedMap) {
        // test for a key in the map
        if (synchronizedMap.containsKey(key)) {
          synchronizedMap.get(key).add(value);
        } else {
          List<String> valuesList = new ArrayList<String>();
          valuesList.add(value);
          // store a value into the map
          synchronizedMap.put(key, valuesList);
       }
    }
    

    This is one of the reasons why the ConcurrentMap interface has the putIfAbsent(K key, V value);. That does not require two operations so you may not need to synchronize around it.

    Btw, I would rewrite the above code to be:

    synchronized (synchronizedMap) {
        // test for a key in the map
        List<String> valuesList = synchronizedMap.get(key);
        if (valueList == null) {
          valuesList = new ArrayList<String>();
          // store a value into the map
          synchronizedMap.put(key, valuesList);
        }
        valuesList.add(value);
    }
    

    Lastly, if most of the operations on the map need to be in a synchronized block anyway, you might as well not pay for the synchronizedMap and just use a HashMap always inside of synchronized blocks.

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

Sidebar

Related Questions

Referring to the other question I asked Django using AJAX with Forms, Views ,
I am using Python for my example, but my question is referring to programmming
This is referring to the question i asked before, and got a really quick
Referring to a question I asked on SuperUser , which is recommended to be
I see that the above question was asked earlier, however even after referring them
Referring to TLB and maintenance issues ... My question to people (often) using the
I am referring as a follow up on this question here: Regular expression to
I'm referring to this great answer given by @Sripathi Krishnan to the question asked
I my previous question I asked how to implement the chat feature using client
Referring to question Spring Autowiring stopped working on GAE I ask: why doesn't AppEngine

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.