| 1 |
<h1>60 Second guide to Multiverse</h1> |
| 2 |
|
| 3 |
Multiverse is a Software Transansactional Memory (STM) implementation and meant as an alternative to traditional lock |
| 4 |
based concurrency control. If you have worked with databases before, STM should feel familiar because |
| 5 |
both share one very important concept: transactions. Mutuable state that needs be accessed transactionally should be stored |
| 6 |
in transactional references (comparible to the atomic references from java.util.concurrent.atomic package). |
| 7 |
</p> |
| 8 |
|
| 9 |
Underneath you can see a small example of an Account with 2 mutable fields: balance and lastUpdate. These 2 fields |
| 10 |
will be updated atomically in the incBalance method using a transaction. |
| 11 |
<pre> |
| 12 |
|
| 13 |
import org.multiverse.api.references.*; |
| 14 |
import static org.multiverse.api.StmUtils.*; |
| 15 |
|
| 16 |
public class Account{ |
| 17 |
|
| 18 |
private final TxnRef<Date> lastUpdate; |
| 19 |
private final TxnInteger balance = newTxnInteger(); |
| 20 |
|
| 21 |
public Account(int balance){ |
| 22 |
this.lastUpdate = newTxnRef<Date>(new Date()); |
| 23 |
this.balance = newTxnInteger(balance); |
| 24 |
} |
| 25 |
|
| 26 |
public void incBalance(final int amount, final Date date){ |
| 27 |
atomic(new Runnable(){ |
| 28 |
public void run(){ |
| 29 |
balance.inc(amount); |
| 30 |
lastUpdate.set(date); |
| 31 |
|
| 32 |
if(balance.get()<<0){ |
| 33 |
throw new IllegalStateException("Not enough money"); |
| 34 |
} |
| 35 |
} |
| 36 |
}); |
| 37 |
} |
| 38 |
} |
| 39 |
</pre> |
| 40 |
The transactional behavior of the incBalance method gives us: |
| 41 |
<ol> |
| 42 |
<li><b>failure atomicity:</b> all writes will commit or none will commit. In this case it could happen that |
| 43 |
money is withdrawn and the balance becomes negative. At the end this will be detected, but the changes are |
| 44 |
already made. The STM will make sure that these changes will never be committed. |
| 45 |
</li> |
| 46 |
<li><b>isolation: </b> all reads and writes are thread-safe and are isolated from |
| 47 |
other transaction. A txn automatically gets a Oracle version of the SERIALIZABLE isolation level. |
| 48 |
You don't need to deal with lower isolation levels like READ_UNCOMMITTED, READ_COMMITTED or |
| 49 |
REPEATABLE_READ. |
| 50 |
</li> |
| 51 |
<li><b>no deadlocks</b> between transactions because deadlocks are prevented by Multiverse. |
| 52 |
</ol> |
| 53 |
|
| 54 |
<h2>Composing transactional methods</h2> |
| 55 |
|
| 56 |
Transactional methods can also be composed in new transactional methods, e.g. |
| 57 |
|
| 58 |
<pre> |
| 59 |
public static void transfer(final Account from, final Account to, final int amount){ |
| 60 |
atomic(new Runnable(){ |
| 61 |
public void run(){ |
| 62 |
Date date = new Date(); |
| 63 |
|
| 64 |
from.incBalance(-amount, date); |
| 65 |
to.incBalance(amount, date); |
| 66 |
} |
| 67 |
}); |
| 68 |
} |
| 69 |
</pre> |
| 70 |
With lock based concurrency control this is dangerous to do since you can quickly run into a deadlock (e.g. when |
| 71 |
2 threads are transferring money from 2 accounts in the opposite direction). But with STM, deadlocks |
| 72 |
will be prevented and transactions retried automatically. |
| 73 |
|
| 74 |
<p/> |
| 75 |
This example only scratches the surface of Multiverse, but just having transactional methods can be |
| 76 |
a very big step forward in a lot of cases. For more advanced features and in-depth documention, the |
| 77 |
<a href="manual.html">manual</a> or <a href="documentationoverview.html">documentation overview</a> |
| 78 |
are the best places to get started. |