net.sourceforge.domian.util.concurrent.locks
Interface Synchronizer

All Known Implementing Classes:
SemaphoreSynchronizer

public interface Synchronizer

Interface for synchronizing arbitrary blocks of code within a JVM process. These blocks of code must be expressed either as Runnables, or as Callables.

All blocks of code synchronized by this class either run in concurrent, or exclusive mode. Concurrent mode means that the blocks will run as normal, possibly in a concurrent manner. They will, however, be gracefully preemptied by blocks running in exclusive mode. Runnables and Callables running in exclusive mode always waits for all other concurrent-mode blocks to finish, then runs exclusively, queuing up all other blocks until it has finished.

Usage example:

public Entity find(final Specification spec) {
  return getSynchronizer().runConcurrently(new Callable<Entity>() {
      public Entity call() {
          // do find...
      }
  });
}

public void persist() {
  getSynchronizer().runExclusively(new Callable<Void>() {
      public Void call() {
          // do persist...
          return null;
      }
  });
}

Since:
0.4
Author:
Eirik Torske

Nested Class Summary
static class Synchronizer.MODE
           
 
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.
 

Method Detail

runConcurrently

<R extends java.lang.Runnable> void runConcurrently(R runnable)
Runs a Runnable in a concurrent manner.

Parameters:
runnable - the runnable to invoke concurrently, managed by this class

callConcurrently

<T,C extends java.util.concurrent.Callable<T>> T callConcurrently(C callable)
Calls a Callable in a concurrent manner.

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

runExclusively

<R extends java.lang.Runnable> void runExclusively(R runnable)
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.

Parameters:
runnable - the runnable to invoke exclusively, managed by this class

callExclusively

<T,C extends java.util.concurrent.Callable<T>> T callExclusively(C callable)
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.

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.