org.multiverse.templates
Class OrElseTemplate<E>

java.lang.Object
  extended by org.multiverse.templates.OrElseTemplate<E>
Type Parameters:
E -

public abstract class OrElseTemplate<E>
extends Object

A template for the 'orelse' functionality provided by the STM. Orelse has to do with blocking and not with clashing transactions. With the or else functionality you are able to block on multiple structures, for example (pseudo code):

 def stack1: new TransactionalStack();
 def stack2: new TransactionalStack();
 

atomic{ return stack1.pop(); }orelse{ return stack2.pop(); }

The possible outcomes are:

  1. an item from stack 1 is popped and returned
  2. an item from stack 2 is popped and returned
  3. the transaction is going to block on stack1 and stack2 and is woken up once an item is placed on either of them. Once the transaction is woken up, it reexecutes the complete transaction.
This functionality is very hard to realize with classic lock based concurrency, but no problem with STM's.

And OrElse templates are allowed to be nested!

A template for the 'orelse' functionality. Example:

 String item = new OrElseTemplate(){
 

String run(Transaction t){ return stack1.pop(); }

String orelscerun(Transaction t){ return stack2.pop(); } }.execute();

If an exception is thrown in the run block, the block is ended but the orelse block is not started and the exception is propagated.

Does not start a transaction if no transaction is found.

Author:
Peter Veentjer.

Constructor Summary
OrElseTemplate()
          Creates a OrElseTemplate using the transaction in the getThreadLocalTransaction.
OrElseTemplate(Transaction tx)
          Creates an OrElseTemplate using the provided transaction.
 
Method Summary
abstract  E either(Transaction tx)
           
 E execute()
           
abstract  E orelse(Transaction tx)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

OrElseTemplate

public OrElseTemplate()
Creates a OrElseTemplate using the transaction in the getThreadLocalTransaction.

Throws:
NullPointerException - if no transaction is found.

OrElseTemplate

public OrElseTemplate(Transaction tx)
Creates an OrElseTemplate using the provided transaction.

Parameters:
tx - the provided transaction.
Throws:
NullPointerException - if is null.
Method Detail

either

public abstract E either(Transaction tx)

orelse

public abstract E orelse(Transaction tx)

execute

public final E execute()


Copyright © 2008-2010 Multiverse. All Rights Reserved.