Using a Mutex Object in Java

1. Overview

In this tutorial, we’ll see different ways to implement a mutex in Java.

2. Mutex

In a multithreaded application, two or more threads may need to access a shared resource at the same time, resulting in unexpected behavior. Examples of such shared resources are data-structures, input-output devices, files, and network connections.

We call this scenario a race condition. And, the part of the program which accesses the shared resource is known as the critical sectionSo, to avoid a race condition, we need to synchronize access to the critical section.

A mutex (or mutual exclusion) is the simplest type of synchronizer – it ensures that only one thread can execute the critical section of a computer program at a time.

To access a critical section, a thread acquires the mutex, then accesses the critical section, and finally releases the mutex. In the meantime, all other threads block till the mutex releases. As soon as a thread exits the critical section, another thread can enter the critical section.

3. Why Mutex?

First, let’s take an example of a SequenceGeneraror class, which generates the next sequence by incrementing the currentValue by one each time:

public class SequenceGenerator {
    
    private int currentValue = 0;

    public int getNextSequence() {
        currentValue = currentValue + 1;
        return currentValue;
    }

}

Now, let’s create a test case to see how this method behaves when multiple threads try to access it concurrently:

@Test
public void givenUnsafeSequenceGenerator_whenRaceCondition_thenUnexpectedBehavior() throws Exception {
    int count = 1000;
    Set<Integer> uniqueSequences = getUniqueSequences(new SequenceGenerator(), count);
    Assert.assertEquals(count, uniqueSequences.size());
}

private Set<Integer> getUniqueSequences(SequenceGenerator generator, int count) throws Exception {
    ExecutorService executor = Executors.newFixedThreadPool(3);
    Set<Integer> uniqueSequences = new LinkedHashSet<>();
    List<Future<Integer>> futures = new ArrayList<>();

    for (int i = 0; i < count; i++) {
        futures.add(executor.submit(generator::getNextSequence));
    }

    for (Future<Integer> future : futures) {
        uniqueSequences.add(future.get());
    }

    executor.awaitTermination(1, TimeUnit.SECONDS);
    executor.shutdown();

    return uniqueSequences;
}

Once we execute this test case, we can see that it fails most of the time with the reason similar to:

java.lang.AssertionError: expected:<1000> but was:<989>
  at org.junit.Assert.fail(Assert.java:88)
  at org.junit.Assert.failNotEquals(Assert.java:834)
  at org.junit.Assert.assertEquals(Assert.java:645)

The uniqueSequences is supposed to have the size equal to the number of times we’ve executed the getNextSequence method in our test case. However, this is not the case because of the race condition. Obviously, we don’t want this behavior.

So, to avoid such race conditions, we need to make sure that only one thread can execute the getNextSequence method at a time. In such scenarios, we can use a mutex to synchronize the threads.

There are various ways, we can implement a mutex in Java. So, next, we’ll see the different ways to implement a mutex for our SequenceGenerator class.

4. Using synchronized Keyword

First, we’ll discuss the synchronized keyword, which is the simplest way to implement a mutex in Java.

Every object in Java has an intrinsic lock associated with it. The synchronized method and the synchronized block use this intrinsic lock to restrict the access of the critical section to only one thread at a time.

Therefore, when a thread invokes a synchronized method or enters a synchronized block, it automatically acquires the lock. The lock releases when the method or block completes or an exception is thrown from them.

Let’s change getNextSequence to have a mutex, simply by adding the synchronized keyword:

public class SequenceGeneratorUsingSynchronizedMethod extends SequenceGenerator {
    
    @Override
    public synchronized int getNextSequence() {
        return super.getNextSequence();
    }

}

The synchronized block is similar to the synchronized method, with more control over the critical section and the object we can use for locking.

So, let’s now see how we can use the synchronized block to synchronize on a custom mutex object:

public class SequenceGeneratorUsingSynchronizedBlock extends SequenceGenerator {
    
    private Object mutex = new Object();

    @Override
    public int getNextSequence() {
        synchronized (mutex) {
            return super.getNextSequence();
        }
    }

}

5. Using ReentrantLock

The ReentrantLock class was introduced in Java 1.5. It provides more flexibility and control than the synchronized keyword approach.

Let’s see how we can use the ReentrantLock to achieve mutual exclusion:

public class SequenceGeneratorUsingReentrantLock extends SequenceGenerator {
    
    private ReentrantLock mutex = new ReentrantLock();

    @Override
    public int getNextSequence() {
        try {
            mutex.lock();
            return super.getNextSequence();
        } finally {
            mutex.unlock();
        }
    }
}

6. Using Semaphore

Like ReentrantLock, the Semaphore class was also introduced in Java 1.5.

While in case of a mutex only one thread can access a critical section, Semaphore allows a fixed number of threads to access a critical section. Therefore, we can also implement a mutex by setting the number of allowed threads in a Semaphore to one.

Let’s now create another thread-safe version of SequenceGenerator using Semaphore:

public class SequenceGeneratorUsingSemaphore extends SequenceGenerator {
    
    private Semaphore mutex = new Semaphore(1);

    @Override
    public int getNextSequence() {
        try {
            mutex.acquire();
            return super.getNextSequence();
        } catch (InterruptedException e) {
            // exception handling code
        } finally {
            mutex.release();
        }
    }
}

7. Using Guava’s Monitor Class

So far, we’ve seen the options to implement mutex using features provided by Java.

However, the Monitor class of Google’s Guava library is a better alternative to the ReentrantLock class. As per its documentation, code using Monitor is more readable and less error-prone than the code using ReentrantLock.

First, we’ll add the Maven dependency for Guava:

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>28.0-jre</version>
</dependency>

Now, we’ll write another subclass of SequenceGenerator using the Monitor class:

public class SequenceGeneratorUsingMonitor extends SequenceGenerator {
    
    private Monitor mutex = new Monitor();

    @Override
    public int getNextSequence() {
        mutex.enter();
        try {
            return super.getNextSequence();
        } finally {
            mutex.leave();
        }
    }

}

8. Conclusion

In this tutorial, we’ve looked into the concept of a mutex. Also, we’ve seen the different ways to implement it in Java.

As always, the complete source code of the code examples used in this tutorial is available over on GitHub.

Related posts:

So sánh HashSet, LinkedHashSet và TreeSet trong Java
Java Program to Solve any Linear Equations
Java Program to Find Nearest Neighbor for Static Data Set
Java Program to Implement the RSA Algorithm
Java Program to Solve TSP Using Minimum Spanning Trees
How to Read a File in Java
Java Program to Implement Graph Coloring Algorithm
Spring Boot - Thymeleaf
Java Program to Solve a Matching Problem for a Given Specific Case
Giới thiệu Google Guice – Injection, Scope
Functional Interfaces in Java 8
Java Program to Perform the Sorting Using Counting Sort
Java Program to Use the Bellman-Ford Algorithm to Find the Shortest Path
Spring Boot Tutorial – Bootstrap a Simple Application
Java Program to Implement Shell Sort
Copy a List to Another List in Java
Java Program to Find the Minimum value of Binary Search Tree
Jackson Date
Spring Boot - Cloud Configuration Client
Spring Boot Gradle Plugin
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Một số từ khóa trong Java
Hướng dẫn Java Design Pattern – Service Locator
Spring Webflux and CORS
The “final” Keyword in Java
Tạo ứng dụng Java RESTful Client không sử dụng 3rd party libraries
Lớp LinkedHashMap trong Java
Làm thế nào tạo instance của một class mà không gọi từ khóa new?
Lớp Arrarys trong Java (Arrays Utility Class)
REST Web service: Tạo ứng dụng Java RESTful Client với Jersey Client 2.x
How to Set TLS Version in Apache HttpClient
Kiểu dữ liệu Ngày Giờ (Date Time) trong java