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

The Archive Base Latest Questions

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

I have a static Map private static Map<String, Car> cars = new HashMap<~>() //Map

  • 0

I have a static Map

private static Map<String, Car> cars = new HashMap<~>() //Map holding car objects

I use the variable inside a method like

private static String getCar(String name){
    return cars.get(name);
}

So, if I want to put lock on cars, is it fine to do like below for thread safety.

private static void xyz() {
    synchronized(cars) {
        Car c = getCar("abc");
        c.setColor("Green");
    }
}

Any Suggestions?

  • 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-08T18:06:48+00:00Added an answer on June 8, 2026 at 6:06 pm

    Java’s synchronized keyword can be applied to any object to “acquire a lock” on that object (or “synchronize on” that object) before proceeding. If any other process tries to run code that synchronizes on the same object at the same time, it will block until the process that locked the object exits the synchronized block.

    The object you synchronize on does not have to be actually used in any part of the synchronized block; it could be a simple mutex object whose only purpose is to be locked and unlocked in synchronized blocks. However, it is important to note that simply synchronizing on an object does not prevent other threads from modifying it if they do not also synchronize on that object. It is a programmer/convention-enforced lock rather than a built-in lock, and all of your code that uses a shared object must “agree” to synchronize on it.

    For example, with the code you wrote above, even though your xyz() method synchronizes on cars, you could write another method like this:

    public void changeCar() {
        Car myCar = cars.get("abc");
        myCar.setColor("Blue");
    }
    

    that modifies cars without calling synchronized. It would be possible for this method to modify the “abc” car at the same time as your method xyz() is modifying it (i.e. violate thread safety) because it does not contain any code that invokes synchronized on cars.

    If you want to ensure that your cars map is thread-safe (i.e. never modified by two methods concurrently), you must either

    1. Ensure that any code that modifies cars first calls synchronized(cars)
    2. Use a ConcurrentHashMap, which guarantees that put and get operations on it are thread-safe.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some map with values: private static Map<String, Long> hodnotyUdaju = new HashMap<String,
I have intialized Hash map inside static block, I need to access the hashmap
I am new to generics. Having a Map like private static Map<String, Object> map;
I have map of string and array of strings as: private static Map cacheTimeStamp
I have a map of constants, like this: private static Map<String, Character> _typesMap =
I have a static map that is a private data member. How do I
I have a static std::map<std::string, CreateGUIFunc> in a class that basically holds strings identifying
I have this array of strings private static String[] colorsArray = { #bde876, #ff8581,
VERSION 1 class Doh { private: static std::map<const std::string, const Doh*> someMap; std::string stringValue_;
I have a static map in my application which populates when I click on

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.