Class Semaphore

java.lang.Object
net.sourceforge.jtds.jdbc.Semaphore

public class Semaphore extends Object
Simple semaphore class used to serialize access requests over the network connection.

Based on the code originally written by Doug Lea. Once JDK 1.5 is the standard this class can be replaced by the java.util.concurrent.Sempahore class.

Version:
$Id: Semaphore.java,v 1.1 2004-12-20 15:51:17 alin_sinpalean Exp $
Author:
Mike Hutchinson
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected long
    Current number of available permits.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Semaphore(long initialPermits)
    Create a Semaphore with the given initial number of permits.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Wait until a permit is available, and take one.
    boolean
    attempt(long msecs)
    Wait at most msecs millisconds for a permit.
    long
    Return the current number of available permits.
    void
    Release a permit.
    void
    release(long n)
    Release N permits.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • permits

      protected long permits
      Current number of available permits.
  • Constructor Details

    • Semaphore

      public Semaphore(long initialPermits)
      Create a Semaphore with the given initial number of permits. Using a seed of one makes the semaphore act as a mutual exclusion lock. Negative seeds are also allowed, in which case no acquires will proceed until the number of releases has pushed the number of permits past 0.
  • Method Details

    • acquire

      public void acquire() throws InterruptedException
      Wait until a permit is available, and take one.
      Throws:
      InterruptedException
    • attempt

      public boolean attempt(long msecs) throws InterruptedException
      Wait at most msecs millisconds for a permit.
      Throws:
      InterruptedException
    • release

      public void release()
      Release a permit.
    • release

      public void release(long n)
      Release N permits. release(n) is equivalent in effect to:
         for (int i = 0; i < n; ++i) release();
       

      But may be more efficient in some semaphore implementations.

      Throws:
      IllegalArgumentException - if n is negative
    • permits

      public long permits()
      Return the current number of available permits. Returns an accurate, but possibly unstable value, that may change immediately after returning.