33146

Java unlimited semaphore

Question:

Wondering how to <strong>not</strong> limit connections (or anything) using a Semaphore.

So you might be thinking, "That sounds dumb." But, it simplifies my code a bit as it lets me treat the limited and unlimited cases uniformly.

Note I'm not looking for an advice on how to write something like

if(limited) { semaphore.acquire(); }

I can come up with dozens of ways to do this forking with if-statements.

More specifically I'm looking for an Apache Commons or Java solution. This is just a simple situation in which I can write my own simple class to solve it, but when there are widely available utility solutions I prefer to use these.

Answer1:

Given that Semaphore is a class, not an interface, you will be forced to have some form of branching in the logic. In order to avoid sprinkling "if (flag)" checks all around your code, you could create an interface for use in your application that includes the acquire and release semantics of the Semaphore class. From that point, provide two implementations, one that is essentially a no-op, providing no protection whatsoever, and another class that delegates to java.util.concurrent.Semaphore - from this point you are in a position to use dependency injection to determine which implementation to use.

Again, the branching inevitably has to live someplace, this just moves it up and out of the business logic.

Recommend

  • Regex of a string [duplicate]
  • A slot can take less arguments than provided by the signal, HOW? - Qt
  • How to add a tab in PySide
  • How do I make a system call and resume execution without waiting for the call to return?
  • MvvmCross: How do I use the MvxTableViewController with a nibname?
  • What's the difference between maven building and forking a project in a multi module build?
  • Java: How do I use the result of the first of multiple threads that complete?
  • ConcurrentHashMap read while resizing
  • Test fails then succeeds
  • get the column name during runtime with entityframework
  • How to create a Plone 4 group who's sole purpose is to manage users?
  • How to fix this iterative python code and reduce repetition?
  • JavaFX/SWT WebView synchronous loadcontent()
  • What happens when you initialize a parameter? C++
  • git fork repo to same organization
  • How to prevent a ResultSet from being invalidated on Connection close?
  • Java out of memory errors on large WSDLs (play framework)
  • How should an app react when indexedDB is blocked
  • the five top rows of the oval are erased. why?
  • Grails Elastic Search Plugin SEVERE: Unexpected transforming call sites grails elasticsearch
  • How to create a thread in a class?
  • Delete ARP entry on Windows CE 6.0 CF.NET 2.0
  • WSO2 ESB 4.0.3 - Error installing Data Services feature from 4.0.* repositories
  • How to make a user wait with Laravel
  • Where in the relevant specification is it documented that some comments in a SQL script are, in fact
  • Generating anchors with PyYAML.dump()?
  • OpenGL - Object Transformations and VBOs
  • Get Currently Active User in Android
  • File extension of zlib zipped html page?
  • iOS Cordova first plugin - plugin.xml to inject a feature
  • I am receiving HibernateException “No Hibernate Session bound to thread, and configuration does not
  • How can Delete be both a DDL and a DML statement
  • pyodbc doesn't report sql server error
  • Tamper-proof configuration files in .NET?
  • Problem deserializing objects from cache on MyBatis 3/Java
  • Swift: Switch statement fallthrough behavior
  • Cannot connect to cassandra from Spark
  • Javascript + PHP Encryption with pidCrypt
  • WOWZA + RTMP + HTML5 Playback?
  • How does Linux kernel interrupt the application?