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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:45:54+00:00 2026-06-06T01:45:54+00:00

The following class acts as a simple cache that gets updated very infrequently (say

  • 0

The following class acts as a simple cache that gets updated very infrequently (say e.g. twice a day) and gets read quite a lot (up to several times a second). There are two different types, a List and a Map. My question is about the new assignment after the data gets updated in the update method. What’s the best (safest) way for the new data to get applied?

I should add that it isn’t necessary for readers to see the absolute latest value. The requirements are just to get either the old or the new value at any given time.

public class Foo {

    private ThreadPoolExecutor _executor;
    private List<Object> _listObjects = new ArrayList<Object>(0);
    private Map<Integer, Object> _mapObjects = new HashMap<Integer, Object>();
    private Object _mutex = new Object();
    private boolean _updateInProgress;

    public void update() {

        synchronized (_mutex) {
            if (_updateInProgress) {
                return;
            } else {
                _updateInProgress = true;
            }
        }

        _executor.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    List<Object> newObjects = loadListObjectsFromDatabase();
                    Map<Integer, Object> newMapObjects = loadMapObjectsFromDatabase();

                    /*
                     * this is the interesting part
                     */
                    _listObjects = newObjects;
                    _mapObjects = newMapObjects;

                } catch (final Exception ex) {
                    // error handling
                } finally {
                    synchronized (_mutex) {
                        _updateInProgress = false;
                    }
                }
            }
        });
    }

    public Object getObjectById(Integer id) {
        return _mapObjects.get(id);
    }

    public List<Object> getListObjects() {
        return new ArrayList<Object>(_listObjects);
    }


}

As you see, currently no ConcurrentHashMap or CopyOnWriteArrayList is used. The only synchronisation is done in the update method.

Although not necessary for my current problem, it would be also great to know the best solution for cases where it is essential for readers to always get the absolute latest value.

  • 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-06T01:45:56+00:00Added an answer on June 6, 2026 at 1:45 am

    You could use plan synchronization unless you are reading over 10,000 times per second.

    If you want concurrent access I would use on of the concurrent collections like ConcurrentHashMap or CopyOnWriteArrayList. These are simpler to use than synchronizing the collection. (i.e. you don’t need them for performance reasons, use them for simplicity)

    BTW: A modern CPU can perform billions of operations in 0.1 seconds so several times a second is an eternity to a computer.

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

Sidebar

Related Questions

I have the following class called City that contains another class called Detail. public
I have the following class that is persisted using Doctrine, but I don't want
I have the following class that enumerates all COM ports on the local PC,
I want to write a container class that acts like a dictionary (actually derives
I'm trying to write a Python class that acts like some sort of datastore.
I have a need for a container that acts like the following. It has
I have the following section of code that acts on values in <Test></Test> nodes
The following code in a WPF app creates a hyperlink that looks and acts
The following code is an attempt to write a variadic function that acts like
The following class is not thread-safe (as proven in Proving the following code not

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.