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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:28:15+00:00 2026-05-13T07:28:15+00:00

I have these classes that I use to create objects that I want to

  • 0

I have these classes that I use to create objects that I want to store at runtime

Class Person
    String name
    Pet[] pets

Class Pet
    String name
    Person[] owners
    boolean neutered

At first I used these HashMaps to store them

HashMap people
HashMap pets

But I wanted to make the implementation concurrent so I changed these maps like so

ConcurrentHashMap people
ConcurrentHashMap pets

I used the "compareAndSet in a while loop" pattern to make atomic updates.

But I still had a problem because each person in my People map has associated pets in the Pets map. To keep updates atomic I added ReentrantReadWriteLocks So that I could update People objects simultaneously with associated Pet objects.

ConcurrentHashMap people
ConcurrentHashMap peopleLocks
ConcurrentHashMap pets
ConcurrentHashMap petLocks

Now when I perform an edit on multiple records, I first grab all the write locks, then I make my edits, and finally release the write locks. This ensures no reading while I make the update.

changePetNames(Person person, Pets[] pets, String[] names) {
    // get Person lock
    // get Pet locks
    // make updates
    // release locks
}

neuter(Pets[] pets) {
    // get Pet locks
    // make updates
    // release locks

I then had all my edit methods synchronize on one object, so that competing edits wouldn’t deadlock

private final Object leash = new Object();
changePetNames(Person person, Pets[] pets, String[] names) {
    synchronized(leash) {
        // get Person lock
        // get Pet locks
        // make updates
        // release locks
    }
}

neuter(Pets[] pets) {
    synchronized(leash) {
        // get Pet locks
        // make updates
        // release locks
    }
}

So now I have runtime storage that allows concurrent reads and synchronized writes. My question is whether there’s a way to make the writes concurrent as well while protecting the relationship between people and pets.

  • 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-13T07:28:16+00:00Added an answer on May 13, 2026 at 7:28 am

    Instead of synchronizing on the leash object, you can synchronize on the People person object. This allows concurrent changes to different persons and their pets while blocking simultatneous changes to one person and her pets.

    PS, from the looks of it your locking system seems to be a bit overly complex. On the assumption that People – Pets is a 1 to many relationship, one person can have many pets but any pet only has one owner, only synchonizing on the person object might be all that you need.

    PS2, naming is important and your class names are plural, I think using Person and Pet instead of People and Pets would describe the concepts better making your code easier to understand.

    Edit
    Methods like neuter that only take pets without needing to change data in the owner would, to make them concurrent, synchronize on the pet but that means that:

    • when you edit a person and her pets, you need to synchronize on both the person and the pet to guard against pet only changes
    • sometimes a pet can be locked while a person with that pet also needs to be locked

    The above can lead to deadlock situations when one thread has a pet lock and tries to get a person lock while another thread has the person lock and tries to get the pet lock. My solution would be to synchronize on the owner, even if only the pet needs to be changed, this means that changePetNames and neuter would look like:

    changePetNames(Person person, Pets[] pets, String[] names) {
        synchronized(person) {
            // make updates
        }
    }
    
    neuter(Pets[] pets) {
        for (Pets pet: pets) {
            // make sure pets owner exists
            synchronized(pet.getOwner()) {
                // make updates
            }
        }
    }
    

    This way no deadlocks can occur if you never nest synchronized actions on different persons.

    Edit2
    When owners to pets is a many to many relationship, you would need to synchronize on a represenatation of a unique combination of persons and pets, which would reproduce the write locks you aquire for updates already. My conclusion here would be that the extra synchronization lease is unneeded if you can make sure deadlock does not occur.

    Deadlock occurs if two threads want to aquire a lock that the other has aquired before, so if you can make sure locks are always aquired in the same sequence, this problem cannot ocur.

    If you would add a unique creation ID to both Person and Pet and aquire locks per update set in increasing order always, a deadlock situation cannot occur:

    changePetNames(Person person, Pets[] pets, String[] names) {
        // sort Person ID's
        // get Person lock in ID sequence
        // sort Pet ID's
        // get Pet locks in ID sequence
        // make updates
        // release locks
    }
    

    should do the trick.

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

Sidebar

Ask A Question

Stats

  • Questions 279k
  • Answers 279k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The difference is in the initial compilation time; when your… May 13, 2026 at 3:26 pm
  • Editorial Team
    Editorial Team added an answer After a little while playing around with GCC, I've got… May 13, 2026 at 3:26 pm
  • Editorial Team
    Editorial Team added an answer It can quite easily be done. Just a matter of… May 13, 2026 at 3:26 pm

Related Questions

High Level With StructureMap, Can I define a assembly scan rule that for an
I have a model library (namespace Test.App.Model.EF ) with the Entity Framework implementation in
I'm just learning how to program in Ruby on Rails, and I'm really impressed
I would like to use Castle Windsor 2.0 as the inversion of control container
I have been working on a wrapper for a COM object that can only

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.