Class CommandExecutor<A>

java.lang.Object
io.inverno.mod.base.concurrent.CommandExecutor<A>
Type Parameters:
A - the target state type

public class CommandExecutor<A> extends Object

A lock-free command executor which guarantees the execution of commands in sequence.

It allows to eliminate race conditions when accessing a target holding a shared state.

It is backed by a multiple producer/single consumer queue storing the commands which are executed in the execute(java.util.function.Consumer) method by a calling thread which was able to acquire the lock.

Since:
1.6
Author:
Jeremy Kuhn
  • Constructor Details

    • CommandExecutor

      public CommandExecutor(A target)

      Creates a command executor on the specified target.

      Parameters:
      target - the target state
  • Method Details

    • getTarget

      public A getTarget()

      Returns the target state.

      Returns:
      the target
    • execute

      public void execute(Consumer<A> command)

      Executes the specified command.

      The command is enqueued and eventually executed by the thread which was able to acquire the lock. As result execution can be synchronous or asynchronous depending on the concurrency level (i.e. how many threads are executing commands concurrently).

      Parameters:
      command - the command to execute