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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:36:13+00:00 2026-06-10T16:36:13+00:00

Java Gurus, Currently we have a HashMap<String,SomeApplicationObject> which is being read frequently and modified

  • 0

Java Gurus,

Currently we have a HashMap<String,SomeApplicationObject> which is being read frequently and modified occasionally and we are having issues that during the modification/reloading, Read operation returns null which is not acceptable.

To fix this I have following options:

A. Use ConcurrentHashMap

Which looks like the first choice but the operation which we are talking about is reload() – means clear() followed by replaceAll(). So if the Map is read post clear() and pre replaceAll() it returns null which is not desirable. Even if I synchronize this doesn’t resolves the issue.

B. Create another implementation based upon ReentrantReadWriteLock

Where I would create acquire Write Lock before reload() operation. This seems more appropriate but I feel there must be something already available for this and I need not to reinvent the wheel.

What is the best way out?

EDIT Is any Collection already available with such feature?

  • 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-10T16:36:14+00:00Added an answer on June 10, 2026 at 4:36 pm

    It seems you are not sure as to how what Peter Lawrey suggests can be implemented. It could look like this:

    class YourClass {
        private volatile Map<String, SomeApplicationObject> map;
    
        //constructors etc.
    
        public void reload() {
            Map<String,SomeApplicationObject> newMap = getNewValues();
            map = Collections.unmodifiableMap(newMap);
        }
    }
    

    There are no concurrency issues because:

    • The new map is created via a local variable, which by definition is not shared – getNewValues does not need to be synchronized or atomic
    • The assignement to map is atomic
    • map is volatile, which guarantees that other threads will see the change
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Unix gurus! I have a Java program which passes some parameters to a Servlet
Calling all MQ Gurus, I have a box under my desk which we use
I have a switch statement in Java, on an Enum which let us call
I'm not any jersey guru but i read that jersey cannot resolve java methods
Hi I'm a PHP developer and I have some experience with Java. I'm trying
I am having one problem in java arraylist. I am good in databases :)
I have a bit of Java code that outputs an XML file to a
I have been using Java for a long time, and for quite some time
Possible Duplicate: Does an open-ended interval implementation exist for Java? i have an int
I got another nooby question to Java gurus Basically what I want is: Take

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.