In this post, We will talk and learn about Semaphore in Java using an Example.
A counting Semaphore is from java.util.concurrent package which maintains a set of permits.
Each call of acquire() blocks if necessary until a permit is available, and then takes it. Each call of release() adds a permit.
The Semaphore usually keeps a count of the number available and acts accordingly.
Semaphores are usually used to restrict the number of threads than can access some (physical or logical) resources. For example, A semaphore to control access to a pool of items.
Connection.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
package com.infotech.connections; import java.util.concurrent.Semaphore; /** * @author KK JavaTutorials *Semaphore is used to maintain limited * Connections in pool */ public class Connection { private static Connection INSTANCE = new Connection(); private int noOfCoonections; private Semaphore semaphore = new Semaphore(10, true); private Connection() { } public static Connection getConnection() { return INSTANCE; } public void connect() { try { semaphore.acquire(); synchronized (this) { noOfCoonections++; System.out.println("Current connection:"+noOfCoonections); } Thread.sleep(2000); synchronized (this) { noOfCoonections--; } } catch (Exception e) { e.printStackTrace(); }finally { if(semaphore != null) semaphore.release(); } } } |
ClientTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package com.infotech.client; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import com.infotech.connections.Connection; /** * @author KK JavaTutorials *This Client Program is used to create *Connections using ExecutorService */ public class ClientTest { public static void main(String args[]) { ExecutorService executorService = null; try { executorService = Executors.newCachedThreadPool(); for (int i = 1; i <=300; i++) { executorService.submit(new Runnable() { @Override public void run() { Connection.getConnection().connect(); } }); } } catch (Exception e) { e.printStackTrace(); }finally { if(executorService != null) executorService.shutdown(); } } } |
Output of this Program:
Current connection:1
Current connection:2
Current connection:3
Current connection:4
Current connection:5
Current connection:6
Current connection:7
Current connection:8
Current connection:9
Current connection:10
Current connection:1
Current connection:2
Current connection:3
Current connection:4
Current connection:5
Current connection:6
Current connection:7
Current connection:8
Current connection:9
Current connection:10
Above Output shows Semaphore always maintains 10 connections in Pool
You May Also Like:
How would you round a double value to certain decimal Precision and Scale ?
What is BlockingQueue? How can we implement Producer and Consumer problem using Blocking Queue?
How to get number of available processors in Java ?
That’s all about Semaphore Example in Java?
If you have any feedback or suggestion please feel free to drop in below comment box.