Coder Social home page Coder Social logo

stm's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.