May 302017

ADM

Thread Semaphore and Mutex in Java

  • 30 May 2017
  • ADM

 

Thread Semaphore and Mutex in Java - images/logos/java.jpg

 

In this article will present a Java multi threads example to show you how to use a Semaphore and a Mutex to limit the number of threads to access a specific resource.

  • Semaphore - usually is referring when is needed to restrict the number of threads that can access a resource. Example, limit max 10 connections to access a file simultaneously or limit to 10 connections to access a database query.
  • Mutex - usually is referring when is needed only one thread to access a resource. Example, limit to one thread to access a file for writing or limit to one connection to modify a database table.

Semaphore

Example

In this example will use limit the access to 3 max slots.

package com.admfactory.threads;

import java.util.concurrent.Semaphore;

public class SemaphoreExample {
    
    /** create the semaphore with three maximum simultaneous threads */
    private static Semaphore semaphore = new Semaphore(3);

    static class MyThread extends Thread {

	private String name = "";

	public MyThread(String name) {
	    this.name = name;
	}

	@Override
	public void run() {
	    try {
		System.out.println(name + " : available Semaphore slots before: " + semaphore.availablePermits());
		System.out.println(name + " : acquiring lock.");
		semaphore.acquire();
		System.out.println(name + " : got the free slot!");

		Thread.sleep(1000);

		System.out.println(name + " : releasing lock.");
		semaphore.release();
		System.out.println(name + " : available Semaphore splots after: " + semaphore.availablePermits());
	    } catch (Exception e) {
		e.printStackTrace();
	    }
	}
    }

    public static void main(String[] args) {
	System.out.println("Semaphore Example");
	System.out.println();
	System.out.println("Total available slots: " + semaphore.availablePermits());

	MyThread thread1 = new MyThread("Thread 1");
	thread1.start();

	MyThread thread2 = new MyThread("Thread 2");
	thread2.start();

	MyThread thread3 = new MyThread("Thread 3");
	thread3.start();

	MyThread thread4 = new MyThread("Thread 4");
	thread4.start();

	MyThread thread5 = new MyThread("Thread 5");
	thread5.start();
    }
}

Output

Semaphore Example

Total available slots: 3
Thread 1 : available Semaphore slots before: 3
Thread 2 : available Semaphore slots before: 3
Thread 3 : available Semaphore slots before: 3
Thread 1 : acquiring lock.
Thread 3 : acquiring lock.
Thread 4 : available Semaphore slots before: 3
Thread 4 : acquiring lock.
Thread 2 : acquiring lock.
Thread 4 : got the free slot!
Thread 3 : got the free slot!
Thread 5 : available Semaphore slots before: 2
Thread 1 : got the free slot!
Thread 5 : acquiring lock.
Thread 4 : releasing lock.
Thread 1 : releasing lock.
Thread 4 : available Semaphore splots after: 2
Thread 2 : got the free slot!
Thread 3 : releasing lock.
Thread 5 : got the free slot!
Thread 1 : available Semaphore splots after: 2
Thread 3 : available Semaphore splots after: 1
Thread 2 : releasing lock.
Thread 5 : releasing lock.
Thread 2 : available Semaphore splots after: 2
Thread 5 : available Semaphore splots after: 3

Based on the output you can notice that Thread 2 got the free slot after Thread 5 already release the lock, this is because there were already three threads that were running.

Mutex

Example

For Mutex will take the same example the only difference will be that Semaphore object will be created with 1 as the parameter value.

private static Semaphore semaphore = new Semaphore(1);

Output

Semaphore Example

Total available slots: 1
Thread 1 : available Semaphore slots before: 1
Thread 2 : available Semaphore slots before: 1
Thread 2 : acquiring lock.
Thread 1 : acquiring lock.
Thread 3 : available Semaphore slots before: 1
Thread 4 : available Semaphore slots before: 0
Thread 2 : got the free slot!
Thread 4 : acquiring lock.
Thread 5 : available Semaphore slots before: 0
Thread 5 : acquiring lock.
Thread 3 : acquiring lock.
Thread 2 : releasing lock.
Thread 2 : available Semaphore splots after: 1
Thread 1 : got the free slot!
Thread 1 : releasing lock.
Thread 1 : available Semaphore splots after: 1
Thread 4 : got the free slot!
Thread 4 : releasing lock.
Thread 4 : available Semaphore splots after: 1
Thread 5 : got the free slot!
Thread 5 : releasing lock.
Thread 5 : available Semaphore splots after: 1
Thread 3 : got the free slot!
Thread 3 : releasing lock.
Thread 3 : available Semaphore splots after: 1

You can notice the difference in the output: from the second "got the free slot!" the message appears only after a "releasing lock." message. that means only one thread is running at the time.

 

References