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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:31:01+00:00 2026-06-13T05:31:01+00:00

I have below class structures in my project for class A and class B

  • 0

I have below class structures in my project for class A and class B:

class A {
    private String id;
    private List<B> bs = new ArrayList<B>();

    A(String id){
        this.id = id;
    }

    public List<B> getBs(){
        return bs;
    }

    public void setBs(List<B> bs){
        this.bs=bs;
    }

    public void addBs(List<B> bs){
        this.bs.addAll(bs);
    }

    public void addB(B b){
        this.bs.add(b);
    }
}

class B {
    private String id;
    private List<String> tags;

    B(String id){
        this.id = id;
    }

    public List<String> getTags(){
        return tags;
    }

    public void setTags(List<String> tags){
        this.tags = tags;
    }

    public void addTags(List<String> tags){
        this.tags.addAll(tags);
    }

    public void addTag(String tag){
        this.tags.add(tag);
    }
}

Also we have a cache class:

class CacheService {
    private static final ConcurrentHashMap<String, Object> CACHE = new ConcurrentHashMap<String, Object>();

    public static Object get(String id){
        return CACHE.get(id);
    }

    public static void put(String id, Object obj){
        return CACHE.put(id, obj);
    }
}

Now objects for class A and B are created using unique IDs and put in this cache by <id, object> combination. For example:

A a1 = new A("10");
CacheService.put("10", a1);

A a2 = new A("11");
CacheService.put("11", a2);

B b1 = new B("1");
CacheService.put("1", b1);

B b2 = new B("2");
CacheService.put("2", b2);

B b3 = new B("3");
CacheService.put("3", b3);

B b4 = new B("4");
CacheService.put("4", b4);

Also I am putting class B objects in the List<B> inside objects a1 and a2. It is important to note that a unique B object is only put once in any A object:

a1.add(b1);
a1.add(b2);

a2.add(b3);
a2.add(b4);

This way we can have multiple objects for class A and B in the CACHE.

Scenario: Now multiple threads access this CACHE but some of them end up to get class A objects and others class B objects depending upon ID specified by user. These threads actually want to read or update information on these objects.

Question: My requirement is when a thread has accessed an object of class A (for example a1) to update it then no other thread should be able to read or update a1 as well as all class B objects (b1 and b2 in this case) which are added to the List<B> inside object a1 until I am finished with all updates on a1. Please tell me how can I acquire a lock in this scenario?

  • 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-13T05:31:03+00:00Added an answer on June 13, 2026 at 5:31 am

    EDIT2: I was going to complete my answer after question was edited, but I just found the class java.util.concurrent.locks.ReentrantReadWriteLock, which I think fits exactly the needs of user sohanlal.

    This class provides a pair of locks, one for reading operations that can be owned at the same time by several elements and is not-blocking by itself. The second one (the write part) avoids any read or write operation when acquired.

    The solution is then as following:

    1. Add lock to container class A. This class is the true owner of the read-write lock.
    2. Add lock attribute to class B. This class DOES NOT own the lock, but only references it to read-lock it.
    3. When adding a new B object to an A container, provide the B object with a reference to the ReentrantReadWriteLock on the container. When extracting a B object from A, dereference the lock “inherited” from the container.
    4. In class A, write-lock on each operation that modifies the collection of B objects (add, removew…). Optionally (if underlying collection of B elements is not synchronized), read-lock operations that do not modify the collection but access it.
    5. In class B, read-lock on every operation.

    Code. This has not been tested, but will give an idea of how it should be implemented:

    class A {
        private String id;
        private List<B> bs = new ArrayList<B>();
        private ReentrantReadWriteLock lk = new ReentrantReadWriteLock();
    
        A(String id){
            this.id = id;
        }
    
        public List<B> getBs(){
            lk.readLock().lock(); // acquire the read lock, since this operation does not affect A contents
            return bs;
            lk.readLock().unlock();
        }
    
        public void setBs(List<B> bs){
            lk.writeLock().lock(); // acquire the write lock, which automatically avoids further reading/writing operations
            this.bs=bs;
            for( B elem : bs )
            {
                // internal B elements need a reference to the reading part of the lock
                elem.setLock(lk.readLock()); 
            }
            lk.writeLock().unlock();
        }
    
        public void addBs(List<B> bs){
            [...] // similar approach that in setBs
        }
    
        public void addB(B b){
            [...] // similar approach that in setBs
        }
    
        public void deleteB( B elem ) // Or whatever notation you want
        {            
            lk.writeLock().lock(); // acquire the write lock
            B internalElem = bs.get(bs.indexOf(elem));             
            if( internalElem != null )
            {
                bs.remove(internalElem);
                bs.unsetLock();
            }
            lk.writeLock().unlock();
        }
    }
    
    class B {
        private String id;
        private List<String> tags;
        private Lock lk;
    
        B(String id){
            this.id = id;
            lk = null;
        }
    
        public void setLock(Lock l){ lk = l; } // put additional controls if you want
        public void unsetLock()
        { 
            lk = null; 
        }
    
        private void lockInternal()
        {
            if(lk!=null){ lk.lock(); }
        }
    
        private void unlockInternal()
        {
            if(lk!=null){ lk.unlock(); }
        }
    
        public List<String> getTags(){
            List<String> ref = null;            
            lockInternal();
                [...] //internal operations
            unlockInternal();
            return ref;
        }
    
        public void setTags(List<String> tags){
            [...] // similar approach that in getTags
        }
    
        public void addTags(List<String> tags){
            [...] // similar approach that in getTags
        }
    
        public void addTag(String tag){
            [...] // similar approach that in getTags
        }
    }
    

    ORIGINAL ANSWER:

    Jeff’s answer is a good starting point, since it appears to solve the problem of touching the A-type object, and modifying its composition. However, we still have a problem related with theB-type objects contained on A.
    Suppose we have an A-type object with several B-type elements inside:

    A a1 = new A("10");
    B b1 = new B("myB1");
    B b2 = new B("myB2");
    B b3 = new B("myB3");
    a1.add(b1);
    a1.add(b2);
    a1.add(b3);
    

    The thing is that, if you are operating over a1, you want to lock any operation over b1, b2 and b3. How can you make sure of that?

    The only solution I see is that all the B elements inside an A container share some common Lock variable. At the time of invoking write-related operations over a1, it acquires the lock, avoiding ANY operation over B elements. Note that no operation over a B element needs to actually acquire the lock… only to check if it has been acquired by A-type container.

    A lot of drawbacks/considerations:

    • Performance: every operation over a B object will involve checking the lock (this can be very costly)
    • Operating constraints: if a single B object can be added to several A-type containers, this gets much more complicated
    • Implementation complexity: when you insert a B object inside a1, you have to provide B with the shared lock. When extracted, the lock must be dereferenced. What happens if container A is deleted? You have to take care of that kind of things.

    EDIT: and I forgot to mention that this mechanism, when writing the A-type container, does not “stop” or take into account ongoing operations over the contained B-type elements.

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

Sidebar

Related Questions

I have below code: class Program { static void Main(string[] args) { Task[] tasks
I have a class like the one below: class Foo { private: std::map<std::string, Bar*>
I have a data structure which is as given below: class File { public
I have the code below : public class Anything { public int Data {
Does making the below class final (adding public final class) have any real impact
I have three model classes that look as below: class Model(models.Model): model = models.CharField(max_length=20,
I have a model defined as below: class Example(models.Model): user = models.ForeignKey(User, null=True) other
I have two classes Foo and Bar that Bar extends Foo as below: class
I have two async classes in my application which you can see below class
We have an old application where the relationship is defined as below: class Practice

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.