synchronized

Synchronization with multiple locks

The basic idea is to use separate locks to guard multiple independent state variables of a class, instead of having only one lock in class scope.

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);
    }
    
}

The above implementation of Grocery guards both fruits and vegetables using the base Grocery lock, as the synchronization is done on method scope. Instead of this fat lock, we can use two separate guards, one for each resource (fruits and vegetables).

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);
    }
    
}

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.

Related Article:

Reference: Reduce lock granularity – Concurrency optimization from our JCG partner Adrianos Dadis at Java, Integration and the virtues of source.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button