Package co.stateful

Class Atomic<T>

  • Type Parameters:
    T - Type of result
    All Implemented Interfaces:
    Callable<T>

    @Loggable(1)
    public final class Atomic<T>
    extends Object
    implements Callable<T>
    Atomic block of code.

    This class runs your Callable in a concurrent thread-safe manner, using a lock from Stateful.co. For example:

     Callable<String> origin = new Callable<String>() {
       @Override
       public String call() {
         // fetch it from a thread-critical resource
         // and return
       }
     };
     Lock lock = new RtSttc(new URN("urn:github:12345"), "token")
       .locks().lock("test");
     String result = new Atomic<String>(origin, lock).call();

    If you want to use Runnable instead, try static method Executors.callable(Runnable). If you want to avoid checked exceptions, use callQuietly().

    Since:
    0.6
    See Also:
    Atomic Counters at Stateful.co, Synchronization Between Nodes
    • Constructor Detail

      • Atomic

        public Atomic​(Callable<T> clbl,
                      Lock lck)
        Public ctor (default maximum waiting time of five minutes).
        Parameters:
        clbl - Callable to use
        lck - Lock to use
      • Atomic

        public Atomic​(Callable<T> clbl,
                      Lock lck,
                      String lbl)
        Public ctor (default maximum waiting time of five minutes).
        Parameters:
        clbl - Callable to use
        lck - Lock to use
        lbl - Label to use for locking and unlocking (can be empty)
      • Atomic

        public Atomic​(Callable<T> clbl,
                      Lock lck,
                      String lbl,
                      long maximum)
        Public ctor.
        Parameters:
        clbl - Callable to use
        lck - Lock to use
        lbl - Label to use for locking and unlocking (can be empty)
        maximum - Maximum waiting time
        Since:
        0.8
    • Method Detail

      • callQuietly

        public T callQuietly()
        Call without exception throwing.
        Returns:
        Result
        Since:
        0.9