Home » Java Basics » synchronized » Synchronization with multiple locks

About Byron Kiourtzoglou

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.

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.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar

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

  Subscribe  
Notify of