Java Semaphore example

Java Semaphore example
In this tutorial, we are going to see about Semaphore in java.
Semaphore is a class in java.util.concurrent package introduced in JDK 5. Semaphore basically maintains a set of permits, so there are two methods which are mainly used for semaphore.

  • acquire
  • release
acquire() method is used to get a permit and if no. of permits reaches max allowed permits then thread has to wait to get permit which will be released by some other thread by calling release() method.

Semaphores are generally used to restrict the number of threads to access resources.

Real time examples:

  • Semaphores can be used to restrict number of database connections at a time
  • Semaphores can also be used to bound any collection.

Example:

We will create a class BoundedArrayList which can have only 5 elements at a time. If any thread wants to add more element to the list,  thread will have to wait until any other thread remove elements from the list.
When we add an element to the list, we will call semaphore.acquire and when we remove an element from the list, we will call semaphore.release.
Create a class called BoundedArrayList.
Create a main class BoundedArrayListMain.java
When you run above program, you will get below output:

Explanation:

  • We have created two thread t1 and t2.
  • t1 and t2 both share common list reference ba.
  • When t1 adds 5 elements to the list, available permits became 0.
  • Now t1 waits for another thread to remove elements, so that semaphore have some available permits.
  • Another thread t2 removes elements from the list after waiting for 5 secs.
  • Once t2 removes elements, t1 adds “Tony” to list.

import_contacts

You may also like:

Related Posts

  • Difference between Runnable and Callable in java
    16 December

    Difference between Runnable and Callable in java

    Runnable and Callable interface both are used in the multithreading environment.Callable is available in java.util.concurrent.Callable package and Runnable in java.lang.Thread. Difference between Runnable and Callable interface in java Runnable was introduced in java 1.0 version While Callable is an extended version of Runnable and introduced in java 1.5 to address the limitation of Runnable. Runnable […]

  • 13 June

    Java Executor framework tutorial with example

    Table of ContentsExecutorExecutorServiceScheduledExecutorServiceExecutorsThreadPoolExecutor Java 5 has introduced new framework called Executor Framework for managing threads.We have already seen before how to create a thread. If you have noted, we need to create an object of thread class using new Thread(runnableObject), so we need to create thread object for each task.Imagine a situation where you have […]

  • Java ExecutorCompletionService
    09 June

    Java ExecutorCompletionService

    Table of ContentsWhy you may need to use ExecutorCompletionService:Java ExecutorCompletionService example: In this post, we will see about Java ExecutorCompletionService example. ExecutorCompletionService class implements CompletionService. This class returns Future object in completion order. Why you may need to use ExecutorCompletionService: Let’s understand with the help of scenario: Let’s say you have 5 tasks, you submit it to the […]

  • 05 June

    Java newSingleThreadExecutor example

    Table of ContentsSyntax:Java newSingleThreadExecutor example:Difference between newSingleThreadExecutor and newFixedThreadPool(1): In this tutorial, we will learn about Executor’s newSingleThreadExecutor factory method. In the last tutorial, I have shared an introduction to ThreadPoolExecutor. If you are not aware of concepts of ThreadPoolExecutor, you should go through that first. Executor’s newSingleThreadExecutor factory method : This method returns thread […]

  • 29 May

    Java ScheduledThreadPoolExecutor Example

    Table of ContentsJava ScheduledThreadPoolExecutor Example:ScheduledThreadPoolExecutor’s scheduleAtFixedRate example:ScheduledThreadPoolExecutor’s scheduleWithFixedDelay example: There are multiple ways to schedule a task in java. We have already Java timer to schedule a task but the problem with timers task is that you can execute one task at a time.So if the current task takes longer subsequent job will be delayed. […]

  • Java FutureTask example
    28 May

    Java FutureTask example

    Table of ContentsJava FutureTask example:Explanation: In this tutorial, we will see about Java FutureTask example. FutureTask class has been introduced in JDK 5 with Executor Framework. FutureTask class is the concrete implementation of the Future object and provides methods for start and cancel the task.It also provides method to see if the computation is done […]

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.