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

  • Home
  • SEARCH
  • 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 6581947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:13:05+00:00 2026-05-25T16:13:05+00:00

I have a store of data objects and I wish to synchronize modifications that

  • 0

I have a store of data objects and I wish to synchronize modifications that are related to one particular object at a time.

class DataStore {
    Map<ID, DataObject> objects = // ...
    // other indices and stuff...

    public final void doSomethingToObject(ID id) { /* ...  */ }
    public final void doSomethingElseToObject(ID id) { /* ... */ }
}

That is to say, I do not wish my data store to have a single lock since modifications to different data objects are completely orthogonal. Instead, I want to be able to take a lock that pertains to a single data object only.

Each data object has a unique id. One way is to create a map of ID => Lock and synchronize upon the one lock object associated with the id. Another way is to do something like:

synchronize(dataObject.getId().toString().intern()) {
    // ...
}

However, this seems like a memory leak — the internalized strings may never be collected.

Yet another idea is to synchronize upon the data object itself; however, what if you have an operation where the data object doesn’t exist yet? For example, what will a method like addDataObject(DataObject) synchronize upon?

In summary, how can I write a function f(s), where s is a String, such that f(s)==f(t) if s.equals(t) in a memory-safe manner?

  • 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-05-25T16:13:05+00:00Added an answer on May 25, 2026 at 4:13 pm

    For such case, I normally have 2 level of lock:
    First level as a reader-writer-lock, which make sure update to the map (add/delete) is properly synchronized by treating them as “write”, and access to entries in map is considered as “read” on the map. Once accessed to the value, then synchronize on the value. Here is a little example:

    class DataStore {
        Map<ID, DataObject> objMap = // ...
        ReadWritLock objMapLock = new ReentrantReadWriteLock();
        // other indices and stuff...
        public void addDataObject(DataObject obj) {
            objMapLock.writeLock().lock();
            try {
                // do what u need, u may synchronize on obj too, depends on situation
                objMap.put(obj.getId(), obj);
            } finally {
                objMapLock.writeLock().unlock();
            }
        }
    
        public final void doSomethingToObject(ID id) { 
            objMapLock.readLock().lock();
            try {
                DataObject dataObj = this.objMap.get(id);
                synchronized(dataObj) {
                    // do what u need
                }
            } finally {
                objMapLock.readLock().unlock();
            }
    
        }
    }
    

    Everything should then be properly synchronized without sacrificing much concurrency

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

Sidebar

Related Questions

In my application, I have three collection objects which store data. The data which
I have an object I am using to store document meta data into a
I have classes that store data, and methods to go get data for individual
I have a Moose object module that should accept a relatively large data structure
Games, such as Nintendo DS games, have to store data of their objects, for
I have objects with location data stored in Core Data, I would like to
I have a server where I store data from Mac A and Mac B.
I have created an archive table which will store data for selecting only. Daily
I have been spoiled by either using sql server to store data, or using
We have about 1400 xml files which we use to store data around the

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.