Interface WorkflowSemaphore


public interface WorkflowSemaphore
Workflow semaphore is an alternative to Semaphore that is deterministic and compatible with Temporal's concurrency model. API is designed to be used in a workflow code only. It is not allowed to be used in an activity code.

In Temporal concurrency model, only one thread in a workflow code can execute at a time.

  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Acquires a permit from this semaphore, blocking until one is available.
    void
    acquire(int permits)
    Acquires the given number of permits from this semaphore, blocking until all are available.
    void
    Releases a permit, returning it to the semaphore.
    void
    release(int permits)
    Releases the given number of permits, returning them to the semaphore.
    boolean
    Acquires the given number of permits from this semaphore, only if all are available at the time of invocation.
    boolean
    tryAcquire(int permits)
    Acquires the given number of permits from this semaphore, only if all are available at the time of invocation.
    boolean
    tryAcquire(int permits, Duration timeout)
    Acquires the given number of permits from this semaphore, if all become available within the given waiting time.
    boolean
    Acquires a permit from this semaphore, if one becomes available within the given waiting time.
  • Method Details

    • acquire

      void acquire()
      Acquires a permit from this semaphore, blocking until one is available.

      Acquires a permit, if one is available and returns immediately, reducing the number of available permits by one.

      Throws:
      CanceledFailure - if thread (or current CancellationScope was canceled).
    • acquire

      void acquire(int permits)
      Acquires the given number of permits from this semaphore, blocking until all are available.

      Acquires the given number of permits, if they are available, and returns immediately, reducing the number of available permits by the given amount.

      Parameters:
      permits - the number of permits to acquire
      Throws:
      CanceledFailure - if thread (or current CancellationScope was canceled).
      IllegalArgumentException - if permits is negative
    • tryAcquire

      boolean tryAcquire()
      Acquires the given number of permits from this semaphore, only if all are available at the time of invocation.

      Acquires a permit, if one is available and returns immediately, with the value true, reducing the number of available permits by one.

      Returns:
      true if the permit was acquired and false otherwise
    • tryAcquire

      boolean tryAcquire(Duration timeout)
      Acquires a permit from this semaphore, if one becomes available within the given waiting time.

      Acquires a permit, if one is available and returns immediately, with the value true, reducing the number of available permits by one.

      Parameters:
      timeout - the maximum time to wait for a permit
      Returns:
      true if a permit was acquired and false if the waiting time elapsed before a permit was acquired
      Throws:
      CanceledFailure - if thread (or current CancellationScope was canceled).
    • tryAcquire

      boolean tryAcquire(int permits)
      Acquires the given number of permits from this semaphore, only if all are available at the time of invocation.

      Acquires the given number of permits, if they are available, and returns immediately, with the value true, reducing the number of available permits by the given amount.

      If insufficient permits are available then this method will return immediately with the value false and the number of available permits is unchanged.

      Parameters:
      permits - the number of permits to acquire
      Returns:
      true if the permits were acquired and false otherwise
      Throws:
      IllegalArgumentException - if permits is negative
    • tryAcquire

      boolean tryAcquire(int permits, Duration timeout)
      Acquires the given number of permits from this semaphore, if all become available within the given waiting time.

      Acquires the given number of permits, if they are available and returns immediately, with the value true, reducing the number of available permits by the given amount.

      Parameters:
      permits - the number of permits to acquire
      timeout - the maximum duration to wait for a permit
      Returns:
      true if the permits was acquired and false if the waiting time elapsed before a permit was acquired
      Throws:
      CanceledFailure - if thread (or current CancellationScope was canceled).
      IllegalArgumentException - if permits is negative
    • release

      void release()
      Releases a permit, returning it to the semaphore.

      There is no requirement that a coroutine that releases a permit must have acquired that permit by calling acquire(). Correct usage of a semaphore is established by programming convention in the application.

    • release

      void release(int permits)
      Releases the given number of permits, returning them to the semaphore.

      There is no requirement that a coroutine that releases a permit must have acquired that permit by calling acquire(). Correct usage of a semaphore is established by programming convention in the application.

      Parameters:
      permits - the number of permits to release
      Throws:
      IllegalArgumentException - if permits is negative