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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:05:56+00:00 2026-06-12T20:05:56+00:00

I have a class which contains two Hashmaps with some data. I would like

  • 0

I have a class which contains two Hashmaps with some data. I would like to update/reload data in the maps on a nightly basis (using Quartz job) and want to lock all other threads from reading during data refresh process.

 public class A {

    private Map<String, Object> someMap1 = new ConcurrentHashMap<String, Object>();
    private Map<String, Object> someMap2 = new ConcurrentHashMap<String, Object>();

    public void reloadData() {
        someMap1.clear();
        someMap2.clear();

        // read new data here and re-fill the maps
        ...
    }

    public Object getDataFromMap(String key) {
        // do some logic here and return data from map
        return someObj;
    }
 }

The getDataFromMap() method should be accessible for all ‘reader’ threads without any blocking in case of data refresh not in progress.

On the other hand, the reloadData() method should wait for all ‘readers’ to complete and then block the maps from reading and reload the data.

‘synchronized’ modifier for reloadData() is not a solutions as it blocks all class and all ‘readers’ if they are in progress in getDataFromMap() logic.

  • 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-12T20:05:57+00:00Added an answer on June 12, 2026 at 8:05 pm

    Your requirement perfectly suits for ReentrantReadWriteLock

        private Map<String, Object> someMap1 = new ConcurrentHashMap<String, Object>();
        private Map<String, Object> someMap2 = new ConcurrentHashMap<String, Object>();
        private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
        private final Lock r = rwl.readLock();
        private final Lock w = rwl.writeLock();
    
        public Object getDataFromMap(String key) {
            r.lock();
            try {
                // do some logic here and return data from map
                return someObj;
            } finally {
                r.unlock();
            }
        }
    
        public void reloadData() {
            w.lock();
            try {
                someMap1.clear();
                someMap2.clear();
                // read new data here and re-fill the maps
            } finally {
                w.unlock();
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one class which contains two more classes like hierarchy. When I'm displaying
I have a class LINE which contains two properties of type POINT. I would
I have a BlogStore class which contains two observablecollections like so public class BlogStore
I have a class library project which contains some content files configured with the
I have class like this below shown. which contains the shopping items where the
I have a class Foo which overrides equals() and hashCode() properly. I would like
I have an array filled with instances of a custom class which contains two
I have a Listener class which implements ViewListener (contains two methods, open, close, which
I have a collection which contains two type of objects A & B. Class
Part of the task I have is to create a class, which contains two

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.