Giter Site home page Giter Site logo

adeynack / more-executors Goto Github PK

View Code? Open in Web Editor NEW
0.0 5.0 0.0 89 KB

Specialised executors for the JVM (mainly Java and Kotlin, but also Scala)

License: MIT License

Java 100.00%
java java-8 concurrency concurrency-library executor-service reactive reactive-programming kotlin mutable-state

more-executors's People

Contributors

adeynack avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

more-executors's Issues

SequencedExecutor

Use cases

  • Limit the number of parallel tasks of a similar nature that are executed at the same time to 1.
  • Ensure mutually exclusive "lock" over a mutable resource without blocking
    • Done by configuring this Executor with only 1 parallel task.
    • Example: Protect a mutable state inside of a service

Why not ...

... use Executors.newSingleThreadExecutor();

Because it creates a new thread. This proposition is an Executor that limits without creating any new thread. The thread pool already exist and we want to use it. In the end, the tasks are dispatched to an existing Executor (which does not have to be a pool, but is the typical use).

Specifications

  • Based on an existing executor (composite)

StateGuard - mutateAndProvide

To be done AFTER

  • #1 SequencedExecutor
  • #3 StateGuard.mutate

Next feature

mutateAndProvide

User submits a lambda receiving the actual state and returning both the value to return as a result of the operation and the new value of the state.

return stateGuard.mutateAndProvide(actual -> {
  log.info("Adding user " + user.getName());
  
  List<User> updatedUserList = new ArrayList<User>(actual.getUserList());
  updatedUserList.add(user);
  UserServiceState newState = new UserServiceState(updatedUserList);

  List<String> userNames = new ArrayList(actual.getUserList().stream().map(u -> u.getName());

  return new MutateAndProvideDecision(newState, userNames);
});

StateGuard

Idea

Ensures that state mutation is thread safe by holding the state value itself internally and making it available only when a task is dispatched to it.

This works better if the state is an immutable class using immutable collections, for total encapsulation.

To be done AFTER

  • #1 SequencedExecutor

Main features

Those are main ideas and not final interfaces.

mutate

User submits a lambda receiving the actual state and returning the new value of the state. The method itself returns a CompletionStage<Void> to allow to caller to react upon the completion of the submitted task.

public CompletionStage<Void> addUser(User user) {
    return stateGuard.mutate(actual -> {
        log.info("Adding user " + user.getName());
        List<User> updatedUserList = new ArrayList<User>(actual.getUserList());
        updatedUserList.add(user);
        UserServiceState newState = new UserServiceState(updatedUserList);
        return newState;
    });    
}

provide

User submits a lambda receiving the actual state and returning a value to pass down to the caller of the method.

public CompletionStage<List<String>> getUserNames() {
    return stateGuard.provide((actual) -> {
       new ArrayList(actual.getUserList().stream().map(u -> u.getName())
    });    
}

mutateAndProvide

User submits a lambda receiving the actual state and returning both the value to return as a result of the operation and the new value of the state.

return stateGuard.mutateAndProvide(actual -> {
  log.info("Adding user " + user.getName());
  
  List<User> updatedUserList = new ArrayList<User>(actual.getUserList());
  updatedUserList.add(user);
  UserServiceState newState = new UserServiceState(updatedUserList);

  List<String> userNames = new ArrayList(actual.getUserList().stream().map(u -> u.getName());

  return new MutateAndProvideDecision(newState, userNames);
});

LimitedExecutor

Use cases

  • Limit the number of parallel tasks of a similar nature that are executed at the same time.
    • Example: Ensure that not more than 3 calls to an external service are made at the same time.

Why not ...

... use Executors.newFixedThreadPool

Because it creates new threads. This proposition is an Executor that limits without creating any new thread. The thread pool already exist and we want to use it. In the end, the tasks are dispatched to an existing Executor (which does not have to be a pool, but is the typical use).

Specifications

  • Based on an existing executor (composite)
  • Receive the number of parallel task to be executed at construction time.

Provide overloads without `baseExecutor`

It should be possible to create the current executor without providing a baseExecutor. That should be using the default executor used for the CompletableFuture.

Dummy (lacking a better one) implementation of such a base executor (in Kotlin):

val baseExecutor = Executor { CompletableFuture.runAsync(it) }

StateGuard - provide

To be done AFTER

  • #1 SequencedExecutor
  • #3 StateGuard.mutate

Next feature

provide

User submits a lambda receiving the actual state and returning a value to pass down to the caller of the method.

public CompletionStage<List<String>> getUserNames() {
    return stateGuard.provide((actual) -> {
       new ArrayList(actual.getUserList().stream().map(u -> u.getName())
    });    
}

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.