views:

82

answers:

2

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.

+2  A: 

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.

rsp
I modified the question to show that eventhough there is a relationship between People and Pets, some modifications are not People related. Synchronization on People in this circumstance would levy an extra requirement on modifications that only want access to pets.
Rapier
Your comments have made me realize that I do need a many to many relationship between people and pets so I have modified the question.
Rapier
+1  A: 

I then had all my edit methods synchronize on one object, so that competing edits wouldn't lock each other out.

Now all edits "lock each other out". In what way is this an improvement?

I don't see why you can't simply omit that sychronization. The aquisition of the ReadWrite lock on Person already prevents conflicting concurrent updates (unless a pet changes an owner, which can be dealt with by additional sychronization on the original owner).

Edit: Ah, "lock each other out" means "deadlock" ...

Wikipedia's article on the Dining Philosophers problem discusses several typical techniques for avoiding deadlock. Probably the easiest solution in your case is the "Resource hierarchy solution" with a total order on the objects to synchronize to. For instance, if you lock objects in order of ascending (unique) id, then perform the operation, and then release all locks, a deadlock can not occur. (Because the thread holding the "highest" lock is not impeded by any other thread.)

meriton
My edit routine grabs multiple pets and sometimes multiple people at the same time. So if two editing threads compete for objects A and B there's a chance one thread gets A and another gets B so that they end up deadlocked. The sync around leash fixes this.
Rapier
Ah, now I understand what you mean by "lock each other out". I have edited my answer to include deadlock protection schemes with better concurrency.
meriton
I modified the question to show that eventhough there is a relationship between People and Pets, some modifications are not People related. So while resource hierarchy would work for changePetNames() it becomes an issue for neuter().
Rapier
Just because you don't need to lock People does not mean a "resource hierarchy" does not work - you can lock all people and pets you intend to modify, modify them, and release the locks. All you have to ensure is that all locks are aquired in a specific order. That is, if you don't modify any People you don't have to lock any.
meriton