multiple synchronization locks

Hi,

I was going through below link

http://examples.javacodegeeks.com/java-basics/synchronized/synchronization-with-multiple-locks/


I have not understood difference between two approaches mentioned there. Which one is better what are advantages and disadvantages and practical uses of each one.

Please advise. Any ideas, resources, sample code highly appreciated. thanks in advance
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dpearsonCommented:
He's saying that a method like this:

public synchronized void addVegetable(int index, String vegetable) {
  vegetables.add(index, vegetable);
}

could have been written like this:

public  void addVegetable(int index, String vegetable) {
  synchronized (this) vegetables.add(index, vegetable);
}

which means it locks on "this".  That means it blocks all other methods that are also trying to lock on "this".

If instead we write it like this:

public void addVegetable(int index, String vegetable) {
  synchronized(vegetables) vegetables.add(index, vegetable);
}

then it locks on "this.vegetables".  So it will only block other threads that are trying to get that lock (this.vegetables) not any other lock on the object.

The upshot is, the second method (using smaller locks) will produce more concurrency and faster code.

Doug
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
contactkarthiCommented:
say you have four programs running simultaneously

p1 adds / removes fruits.
p2 adds / removes vegetables.
p3 adds / removes fruits.
p4 adds / removes vegetables.

incase of first code when p1 is trying to add or remove fruits p2,p3,p4 has to wait. same way when p2 is adding or removing vegetables p1,p3,p4 has to wait and so on.

incase of second code when p1 is trying to add or remove fruits only p3 has to wait. same way when p2 is adding or removing vegetables only p4 has to wait and so on.
0
gudii9Author Commented:
>>synchronized (this) vegetables.add(index, vegetable);
I do not see that line in (is it supposed to be synchronized
>>this. vegetables.add(index, vegetable);
or this is not a keywrd there but you put for pointing to that line



public class Grocery {
	
    private final ArrayList fruits = new ArrayList();
    private final ArrayList vegetables = new ArrayList();
	
    public synchronized void addFruit(int index, String fruit) {

  fruits.add(index, fruit);
    }
    
    public synchronized void removeFruit(int index) {

  fruits.remove(index);
    }
    
    public synchronized void addVegetable(int index, String vegetable) {

  vegetables.add(index, vegetable);
    }
    
    public synchronized void removeVegetable(int index) {

  vegetables.remove(index);
    }
    
}

Open in new window



I still did not understand below concept
use separate locks to guard multiple independent state variables of a class, instead of having only one lock in class scope.

what is fat lock?

what is separate guards?

After using two guards (splitting the lock), we will see less locking traffic than the original fat lock would have. This technique works better when we apply it on locks that have medium lock contention.???
>>  synchronized (this) vegetables.add(index, vegetable);


I do not see above line in the link??

Please advice with complete code example which i can run and see.


>>incase of first code when p1 is trying to add or remove fruits p2,p3,p4 has to wait. >>same way when p2 is adding or removing vegetables p1,p3,p4 has to wait and so on.

When you mention first code you mean below piece of code


public class Grocery {
	
    private final ArrayList fruits = new ArrayList();
    private final ArrayList vegetables = new ArrayList();
	
    public synchronized void addFruit(int index, String fruit) {

  fruits.add(index, fruit);
    }
    
    public synchronized void removeFruit(int index) {

  fruits.remove(index);
    }
    
    public synchronized void addVegetable(int index, String vegetable) {

  vegetables.add(index, vegetable);
    }
    
    public synchronized void removeVegetable(int index) {

  vegetables.remove(index);
    }
    
}

Open in new window


When you mention second code you mean below piece of code

public class Grocery {
	
    private final ArrayList fruits = new ArrayList();
    private final ArrayList vegetables = new ArrayList();
	
    public void addFruit(int index, String fruit) {

  synchronized(fruits) fruits.add(index, fruit);
    }
    
    public void removeFruit(int index) {

  synchronized(fruits) {fruits.remove(index);}
    }
    
    public void addVegetable(int index, String vegetable) {

  synchronized(vegetables) vegetables.add(index, vegetable);
    }
    
    public void removeVegetable(int index) {

  synchronized(vegetables) vegetables.remove(index);
    }
    
}

Open in new window




>>>synchronized(fruits) fruits.add(index, fruit);


i do not see add method anywhere???
Please advise
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

gudii9Author Commented:
Please advise
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
>>>synchronized(fruits) fruits.add(index, fruit);

i do not see add method anywhere???
The add method is being called on the "fruits" object, and that fruits object is declared at the top of the code as an ArrayList, so that method in question is...

ArrayList.add(int index, E element)
0
gudii9Author Commented:
public  void addVegetable(int index, String vegetable) {
  synchronized (this) vegetables.add(index, vegetable);
}

which means it locks on "this".  That means it blocks all other methods that are also trying to lock on "this".

If instead we write it like this:

public void addVegetable(int index, String vegetable) {
  synchronized(vegetables) vegetables.add(index, vegetable);
}

then it locks on "this.vegetables".  So it will only block other threads that are trying to get that lock (this.vegetables) not any other lock on the object.

The upshot is, the second method (using smaller locks) will produce more concurrency and faster code.


I did not understand above difference between first and second method/approach you mentioned with 'this' and without 'this'. please elaborate.
0
gudii9Author Commented:
After using two guards (splitting the lock), we will see less locking traffic than the original fat lock would have. This technique works better when we apply it on locks that have medium lock contention.


what the author meant by two guards and medium lock contention.

Please advise
0
gudii9Author Commented:
Please advise
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.