org.apache.tomcat.util.threads

## Class LimitLatch

- java.lang.Object
- - org.apache.tomcat.util.threads.LimitLatch

-     public class LimitLatch
      extends Object

  Shared latch that allows the latch to be acquired a limited number of times after which all subsequent requests to acquire the latch will be placed in a FIFO queue until one of the shares is returned.

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `LimitLatch(long limit)` Instantiates a LimitLatch object with an initial limit.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `long` | `countDown()` Releases a shared latch, making it available for another thread to use.
    `void` | `countUpOrAwait()` Acquires a shared latch if one is available or waits for one if no shared latch is current available.
    `long` | `getCount()` Returns the current count for the latch
    `long` | `getLimit()` Obtain the current limit.
    `Collection<Thread>` | `getQueuedThreads()` Provide access to the list of threads waiting to acquire this limited shared latch.
    `boolean` | `hasQueuedThreads()` Returns `true` if there is at least one thread waiting to acquire the shared lock, otherwise returns `false`.
    `boolean` | `releaseAll()` Releases all waiting threads and causes the `limit` to be ignored until `reset()` is called.
    `void` | `reset()` Resets the latch and initializes the shared acquisition counter to zero.
    `void` | `setLimit(long limit)` Sets a new limit.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### LimitLatch

          public LimitLatch(long limit)

      Instantiates a LimitLatch object with an initial limit.
      Parameters:
        `limit` - - maximum number of concurrent acquisitions of this latch

  - ### Method Detail

    - #### getCount

          public long getCount()

      Returns the current count for the latch
      Returns:
        the current count for latch

    - #### getLimit

          public long getLimit()

      Obtain the current limit.
      Returns:
        the limit

    - #### setLimit

          public void setLimit(long limit)

      Sets a new limit. If the limit is decreased there may be a period where more shares of the latch are acquired than the limit. In this case no more shares of the latch will be issued until sufficient shares have been returned to reduce the number of acquired shares of the latch to below the new limit. If the limit is increased, threads currently in the queue may not be issued one of the newly available shares until the next request is made for a latch.
      Parameters:
        `limit` - The new limit

    - #### countUpOrAwait

          public void countUpOrAwait()
                              throws InterruptedException

      Acquires a shared latch if one is available or waits for one if no shared latch is current available.
      Throws:
        `InterruptedException` - If the current thread is interrupted

    - #### countDown

          public long countDown()

      Releases a shared latch, making it available for another thread to use.
      Returns:
        the previous counter value

    - #### releaseAll

          public boolean releaseAll()

      Releases all waiting threads and causes the `limit` to be ignored until `reset()` is called.
      Returns:
        `true` if release was done

    - #### reset

          public void reset()

      Resets the latch and initializes the shared acquisition counter to zero.
      See Also:
        `releaseAll()`

    - #### hasQueuedThreads

          public boolean hasQueuedThreads()

      Returns `true` if there is at least one thread waiting to acquire the shared lock, otherwise returns `false`.
      Returns:
        `true` if threads are waiting

    - #### getQueuedThreads

          public Collection<Thread> getQueuedThreads()

      Provide access to the list of threads waiting to acquire this limited shared latch.
      Returns:
        a collection of threads

Copyright © 2000-2022 Apache Software Foundation. All Rights Reserved.