net.sourceforge.domian.util.concurrent.locks
Class SemaphoreSynchronizer

java.lang.Object
  extended by net.sourceforge.domian.util.concurrent.locks.SemaphoreSynchronizer
All Implemented Interfaces:
Synchronizer

public final class SemaphoreSynchronizer
extends java.lang.Object
implements Synchronizer

A Synchronizer implementation using two Semaphore instances.

Since:
0.4
Author:
Eirik Torske

Nested Class Summary
 
Nested classes/interfaces inherited from interface net.sourceforge.domian.util.concurrent.locks.Synchronizer
Synchronizer.MODE
 
Field Summary
private  java.util.concurrent.Semaphore concurrentAccessSemaphore
          Semaphore managing permits and blocking queue for concurrent-mode blocks.
private  java.util.concurrent.Semaphore exclusiveAccessSemaphore
          Semaphore managing one single permit and blocking queue for exclusive-mode blocks.
private static boolean FAIR
           
private static java.util.concurrent.atomic.AtomicBoolean hasAcquiredExecutionPermitAtomicBoolean
           
private static java.lang.ThreadLocal<java.lang.Boolean> hasAcquiredExecutionPermitThreadLocal
          Permits must only be acquired once per thread.
private static org.slf4j.Logger log
           
private static int MAX_NUMBER_OF_CONCURRENT_PERMITS
           
private static boolean NON_FAIR
           
 
Constructor Summary
SemaphoreSynchronizer()
           
 
Method Summary
<T,C extends java.util.concurrent.Callable<T>>
T
callConcurrently(C callable)
          Calls a Callable in a concurrent manner.
<T,C extends java.util.concurrent.Callable<T>>
T
callExclusively(C callable)
          Calls a Callable in an exclusive manner.
<R extends java.lang.Runnable>
void
runConcurrently(R runnable)
          Runs a Runnable in a concurrent manner.
<R extends java.lang.Runnable>
void
runExclusively(R runnable)
          Runs a Runnable in an exclusive manner.
private static void setThisThreadHasAcquiredExecutionPermitTo(java.lang.Boolean booleanValue)
           
private static java.lang.Boolean thisThreadHasAlreadyAcquiredExecutionPermit()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

private static final org.slf4j.Logger log

MAX_NUMBER_OF_CONCURRENT_PERMITS

private static final int MAX_NUMBER_OF_CONCURRENT_PERMITS
See Also:
Constant Field Values

FAIR

private static final boolean FAIR

NON_FAIR

private static final boolean NON_FAIR

concurrentAccessSemaphore

private final java.util.concurrent.Semaphore concurrentAccessSemaphore
Semaphore managing permits and blocking queue for concurrent-mode blocks.


exclusiveAccessSemaphore

private final java.util.concurrent.Semaphore exclusiveAccessSemaphore
Semaphore managing one single permit and blocking queue for exclusive-mode blocks.


hasAcquiredExecutionPermitAtomicBoolean

private static final java.util.concurrent.atomic.AtomicBoolean hasAcquiredExecutionPermitAtomicBoolean

hasAcquiredExecutionPermitThreadLocal

private static final java.lang.ThreadLocal<java.lang.Boolean> hasAcquiredExecutionPermitThreadLocal
Permits must only be acquired once per thread. This is needed to prevent deadlock for nested invocations.

Constructor Detail

SemaphoreSynchronizer

public SemaphoreSynchronizer()
Method Detail

thisThreadHasAlreadyAcquiredExecutionPermit

private static java.lang.Boolean thisThreadHasAlreadyAcquiredExecutionPermit()

setThisThreadHasAcquiredExecutionPermitTo

private static void setThisThreadHasAcquiredExecutionPermitTo(java.lang.Boolean booleanValue)

runConcurrently

public <R extends java.lang.Runnable> void runConcurrently(R runnable)
Description copied from interface: Synchronizer
Runs a Runnable in a concurrent manner.

Specified by:
runConcurrently in interface Synchronizer
Parameters:
runnable - the runnable to invoke concurrently, managed by this class

callConcurrently

public <T,C extends java.util.concurrent.Callable<T>> T callConcurrently(C callable)
Description copied from interface: Synchronizer
Calls a Callable in a concurrent manner.

Specified by:
callConcurrently in interface Synchronizer
Parameters:
callable - the callable to invoke concurrently, managed by this class
Returns:
the Callable's return value, or null if no return value

runExclusively

public <R extends java.lang.Runnable> void runExclusively(R runnable)
Description copied from interface: Synchronizer
Runs a Runnable in an exclusive manner. Firstly, it will wait for all other exclusive Runnables and Callables to finish, if any. Then it will wait for already running concurrent Runnables and Callables to finish, if any. Finally it will run, queuing up all other Runnables and Callables managed by the same Synchronizer instance. When done, all queues are released.

Specified by:
runExclusively in interface Synchronizer
Parameters:
runnable - the runnable to invoke exclusively, managed by this class

callExclusively

public <T,C extends java.util.concurrent.Callable<T>> T callExclusively(C callable)
Description copied from interface: Synchronizer
Calls a Callable in an exclusive manner. Firstly, it will wait for all other exclusive Runnables and Callables to finish, if any. Then it will wait for already running concurrent Runnables and Callables to finish, if any. Finally it will run, queuing up all other Runnables and Callables managed by the same Synchronizer instance. When done, all queues are released.

Specified by:
callExclusively in interface Synchronizer
Parameters:
callable - the callable to invoke exclusively, managed by this class
Returns:
the Callable's return value, or null if no return value


Copyright © 2006-2010. All Rights Reserved.