Codelytics

Visualise your geek speak

  • Blog
  • Terms
  • Privacy
  • Contact

4 Reasons to use a Java Semaphore

June 29, 2015 by Adrian Bartlett Leave a Comment

Why is a Java Semaphore useful?  Today I’ll tell you the four reasons why a Java Semaphore is useful.

4 reasons to use Java's Semaphore

This is part two of a series on Java concurrency.  Part one was about the synchronization of threads using CountDownLatch.

Concept

java.util.concurrent.Semaphore can be used to restrict access to finite resources.  The number of concurrent clients accessing a finite resource can be capped with a Counting Semaphore.  Throttling access to finite system resources, improves system stability and performance.

Applications

The four reasons why a Java Counting Semaphore is useful are listed below.  A Java Counting Semaphore can be used to restrict the maximum amount of:

  1. Database connections – to stop disk trashing
  2. TCP/IP output connections – to prevent exhaustion of system resources
  3. Threads – too many threads reduces application performance
  4. Memory – any collections size can be restricted with the use of a Counting Semaphore

API

Pertinent methods are presented:

  • Semaphore(int permits): Permits, is the maximum number of concurrent clients, allowed to access the resource
  • acquire(): Acquires a permit (to access a protected resource).  If a permit is not available this call blocks until a permit is available.
  • release(): Once the client has finished using the resource, the client calls this method to free the resource for others to use

Example

In the following example UrlPool class constrains the maximum number of connections allowed to access an external server.  This stops putting a server under too much load.  The example has been simplified to only allow one connection to the external server, however the maximum number of outbound connections could be set to a higher number.

Click on the flashing arrow to step through the example.

Http iframes are not shown in https pages in many major browsers. Please read this post for details.

image source: ITU images, image modified

Share this:

  • Click to email this to a friend (Opens in new window)
  • Click to print (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on Google+ (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)

Filed Under: Blog posts, Concurrency, Java Tagged With: Collections, Concurrency, Java, Semaphore

Connect with us

  • Facebook
  • Google+
  • Twitter
  • YouTube

Leave a Reply Cancel reply

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

  • Facebook
  • Google+
  • Twitter
  • YouTube

Subscribe to the blog

Recent posts

  • LinkedHashMap Tutorial
  • java.util.TreeMap visualized: interactive view from a new perspective
  • Constructor & Setter Dependency Injection
  • 4 Reasons to use a Java Semaphore
  • Java 8: How To Retrieve Absent Values From HashMap

Search

  • Blog
  • Terms
  • Privacy
  • Contact

All website content, including software, (excluding that owned by Wordpress and Centric Theme on Genesis Framework) is owned by Codelytics Pty Ltd and/or its affiliates, all rights reserved and protected by Australian copyright laws and relevant international treaty provisions. Any reproduction, redistribution and/or reverse engineering is expressly prohibited. For further details see the terms and conditions page on this site.
© Codelytics Pty Ltd, Adrian Bartlett, 2013

Copyright © 2022 · Centric Theme on Genesis Framework · WordPress · Log in

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.