Ilias Tsagklis

About Ilias Tsagklis

Ilias Tsagklis is a senior software engineer working in the telecom domain. He is an applications developer in a wide variety of applications/services. Ilias is co-founder and Executive Editor at Java Code Geeks.

Semaphores example limiting URL connections

In this example we shall show you how to use a Semaphore for limiting URL connections. We have implemented a class, ConnectionLimiter that uses a Semaphore and is described below:

  • It creates a new Semaphore with a given number of permits.
  • It implements a method, URLConnection acquire(URL url). The method acquires a permit from this semaphore, blocking until one is available, with acquire() API method of Semaphore. Then it returns a URLConnection instance that represents a connection to the remote object referred to by the given url.
  • The class also implements the release(URLConnection conn) method, that after doing any clean up operation for the url, releases the acquired permit, returning it to the semaphore,

as described in the code snippet below.

public class ConnectionLimiter {
   private final Semaphore semaphore;
   private ConnectionLimiter(int maxConcurrentRequests) {

 semaphore = new Semaphore(maxConcurrentRequests);
   public URLConnection acquire(URL url) throws InterruptedException,

IOException {


 return url.openConnection();
   public void release(URLConnection conn) {

 try {


     * ... clean up here


 } finally {



This was an example of how to use a Semaphore for limiting URL connections in Java.

Related Article:

Reference: Java Concurrency Part 1 – Semaphores from our JCG partners at the Carfey Software blog

Related Whitepaper:

Java Essential Training

Author David Gassner explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more!

The course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts. Exercise files are included with the course.

Get it Now!  

Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books
Get tutored by the Geeks! JCG Academy is a fact... Join Now
Hello. Add your message here.