以原子方式运行一段代码
import groovyx.gpars.stm.GParsStm import org.multiverse.api.references.TxnIntegerimport static org.multiverse.api.StmUtils.newTxnInteger
public 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(); } } }
自定义事务属性
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 }