groovyx.gpars.util
Class EnhancedSemaphore

java.lang.Object
  extended by java.util.concurrent.Semaphore
      extended by groovyx.gpars.util.EnhancedSemaphore
All Implemented Interfaces:
java.io.Serializable

public class EnhancedSemaphore
extends java.util.concurrent.Semaphore

Extends Semaphore with a handy withSemaphore(Closure) method to safely acquire and release the Semaphore for the passed-in closure. Use: def extendedSemaphore = new ExtendedSemaphore() extendedSemaphore.withSemaphore() {* //semaphore acquired here }*

Author:
Vaclav Pech Date: Jan 8, 2009
See Also:
Serialized Form

Field Summary
private static long serialVersionUID
           
 
Constructor Summary
EnhancedSemaphore(int permits)
          Creates a new EnhancedSemaphore, delegating to the Semaphore class constructor.
 
Method Summary
 void withSemaphore(groovy.lang.Closure cl)
          Performs the passed-in closure with the Semaphore acquired and releases the Semaphore automatically after the closure finishes.
 
Methods inherited from class java.util.concurrent.Semaphore
acquire, acquire, acquireUninterruptibly, acquireUninterruptibly, availablePermits, drainPermits, getQueuedThreads, getQueueLength, hasQueuedThreads, isFair, reducePermits, release, release, toString, tryAcquire, tryAcquire, tryAcquire, tryAcquire
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values
Constructor Detail

EnhancedSemaphore

public EnhancedSemaphore(int permits)
Creates a new EnhancedSemaphore, delegating to the Semaphore class constructor.

Parameters:
permits - Maximum number of concurrently accepted threads.
Method Detail

withSemaphore

public void withSemaphore(groovy.lang.Closure cl)
                   throws java.lang.InterruptedException
Performs the passed-in closure with the Semaphore acquired and releases the Semaphore automatically after the closure finishes.

Parameters:
cl - The closure to perform with the Semaphore acquired
Throws:
java.lang.InterruptedException - If the current thread gets interrupted

Copyright © 2008–2010 Václav Pech. All Rights Reserved.