since 2021-05-05 18:20
STM (Software Transactional Memory) is one kind of concurrency control mechanism like locks or actors, inspired by Database System Transaction 'ACID' (Atomicity, Consistency, Isolation, Durability).
This is a demo implemented in Java.
- Fork this repository and clone.
- Run example located under test package.
You can also create your test case like this:
public class Account {
STMTransaction<Long> tx = new STMTransaction<>();
private final TransactionReference<Long> txnRef;
public AccountSTM(Long balance) {
txnRef = new TransactionReference<>(balance);
}
public TransactionReference<Long> getRef() {
return txnRef;
}
public Long getBalance() {
return txnRef.getValue(tx);
}
public void transferTo(final Account target, final Long amount) {
STM.<Long>atomic(
txn -> {
Long oldFrom = this.getRef().getValue(txn);
this.getRef().setValue(oldFrom - amount, txn);
Long oldTo = ((AccountSTM) target).getRef().getValue(txn);
((AccountSTM) target).getRef().setValue(oldTo + amount, txn);
}
);
}
public static void main(String[] args) {
Account account = new Account(0L);
final CountDownLatch countDownLatch = new CountDownLatch(1000);
ExecutorService executorService = Executors.newCachedThreadPool();
for (int i = 0; i < 1000; i++) {
executorService.execute(() -> {
Account x = new Account(amount);
x.transferTo(account, amount);
countDownLatch.countDown();
});
}
countDownLatch.await();
executorService.shutdown();
}
}
Simple micro benchmarking with JMH (compared to Atomic API):
Benchmark Mode Cnt Score Error Units
Benchmark.atomicTest avgt 5 883.999 ± 22.208 us/op
Benchmark.stmTest avgt 5 992.872 ± 71.084 us/op
If you want to learn more about STM and other concurrency models, please click in the following links:
See LICENSE file.