(Quick Reference)
Running a piece of code atomically
import groovyx.gpars.stm.GParsStm
import org.multiverse.api.references.TxnIntegerimport static org.multiverse.api.StmUtils.newTxnIntegerpublic class Account {
private final TxnInteger amount = newTxnInteger(0); public void transfer(final int a) {
GParsStm.atomic {
amount.increment(a);
}
} public int getCurrentAmount() {
GParsStm.atomicWithInt {
amount.get();
}
}
}
Customizing the transactional properties
import groovyx.gpars.stm.GParsStm
import org.multiverse.api.AtomicBlock
import org.multiverse.api.PropagationLevelfinal TxnExecutor block = GParsStm.createTxnExecutor(maxRetries: 3000, familyName: 'Custom', PropagationLevel: PropagationLevel.Requires, interruptible: false)
assert GParsStm.atomicWithBoolean(block) {
true
}