Multiverse is a Java 1.6+ based Software Transactional Memory (STM) framework. One of the main goals is to gain new insights in this very interesting new direction of concurrency control, but also to make it easy to create powerful, easy to use and good performing/scalable threadsafe datastructures. For a list of features (and planned features) check the Wiki.
Multiverse supports multiple programming models. The most used model is based on POJO's combined with annotations. This is an example of how simple concurrency control could be:
@AtomicObject
public class Stack<E>{
private Node<E> head;
public void push(E item) {
if (item == null) {
throw new NullPointerException();
}
head = new Node(item, head);
}
public E peek() {
return head == null ? null : head.value;
}
public E pop() {
if (head == null){
retry();
}
return removeTopItem();
}
private E removeTopItem() {
Node<E> oldHead = head;
head = head.parent;
return oldHead.value;
}
public int size() {
return head == null ? 0 : head.size();
}
public boolean isEmpty() {
return size() == 0;
}
public static class Node<E> {
final E value;
final Node parent;
final int size;
Node(E value, Node prev) {
this.value = value;
this.parent = prev;
this.size = parent == null ? 1 : prev.size+1;
}
int size() {
return size;
}
}
}As you can see there is no explicit need for volatile variables, synchronized blocks/locks and wait/notify mechanisms or conditions. By default all instance method of an AtomicObject are made atomic themselves, so all reads/writes done will be isolated from others and all changes will make it, or none will make it (failure atomicity).
Just like JPA/Hibernate (Multiverse tries to mimic Hibernate semantics where possible), each transaction will receive its transaction local content of an atomic object. So since there is no shared state, there are no concurrency issues. The STM is going to worry about conflicts when the changes are committed.
For more information about how to use Multiverse, see Introduction to annotation based configuration.
At the moment only 0.3 SNAPSHOT releases are available. For more information see the the following page on the Wiki.