Skip to content

Latest commit

 

History

History
78 lines (64 loc) · 2.68 KB

README.md

File metadata and controls

78 lines (64 loc) · 2.68 KB

STM

License Java

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.

Quick Start

  1. Fork this repository and clone.
  2. 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

Links

If you want to learn more about STM and other concurrency models, please click in the following links:

License

See LICENSE file.