Module stormpot

Class BlazePoolVirtualThreadSafeTap<T extends Poolable>

java.lang.Object
stormpot.internal.BlazePoolVirtualThreadSafeTap<T>
Type Parameters:
T - The poolable type.
All Implemented Interfaces:
PoolTap<T>

public final class BlazePoolVirtualThreadSafeTap<T extends Poolable> extends Object implements PoolTap<T>
The claim method in this pool tap offers similar thread-safety and performance to the default thread-safe pool tap, but without the use of ThreadLocal.

This allows the pool tap to be used by virtual threads, without risking the undue memory overhead of thousands or millions of thread-locals.

  • Constructor Details

    • BlazePoolVirtualThreadSafeTap

      public BlazePoolVirtualThreadSafeTap(BlazePool<T> pool, boolean optimizeForMemory)
      Create a virtual-thread-safe tap for the given pool.
      Parameters:
      pool - The pool to tap from.
      optimizeForMemory - true to optimise for lower memory usage, otherwise false to prioritize performance.
  • Method Details

    • claim

      public T claim(Timeout timeout) throws PoolException, InterruptedException
      Description copied from interface: PoolTap
      Claim the exclusive rights until released, to an object in the pool. Possibly waiting up to the specified amount of time, as given by the provided Timeout instance, for one to become available if the pool has been depleted. If the timeout elapses before an object can be claimed, then null is returned instead. The timeout will be honoured even if the Allocators allocate methods blocks forever. If the given timeout has a zero or negative value, then the method will not wait.

      If the current thread has already one or more objects currently claimed, then a distinct object will be returned, if one is or becomes available. This means that it is possible for a single thread to deplete the pool, if it so desires. However, doing so is inherently deadlock prone, so avoid claiming more than one object at a time per thread, if at all possible.

      This method may throw a PoolException if the pool have trouble allocating objects. That is, if its assigned Allocator throws exceptions from its allocate method, or returns null.

      An InterruptedException will be thrown if the thread has its interrupted flag set upon entry to this method, or is interrupted while waiting. The interrupted flag on the thread will be cleared after this, as per the general contract of interruptible methods.

      If the pool has been shut down, then an IllegalStateException will be thrown when this method is called. Likewise, if we are waiting for an object to become available, and someone shuts the pool down.

      Here's an example code snippet, where an object is claimed, printed to System.out, and then released back to the pool:

      Poolable obj = pool.claim(TIMEOUT);
      if (obj != null) {
        try {
          System.out.println(obj);
        } finally {
          obj.release();
        }
      }
      

      Memory effects:

      • The release of an object happens-before any subsequent claim or deallocation of that object, and,
      • The allocation of an object happens-before any claim of that object.
      Specified by:
      claim in interface PoolTap<T extends Poolable>
      Parameters:
      timeout - The timeout of the maximum permitted time-slice to wait for an object to become available. A timeout with a value of zero or less, means that the call will do no waiting, preferring instead to return early if no objects are available.
      Returns:
      An object of the Poolable subtype T to which the exclusive rights have been claimed, or null if the timeout period elapsed before an object became available.
      Throws:
      PoolException - If an object allocation failed because the Allocator threw an exception from its allocate method, or returned null, or the expiration check threw an exception.
      InterruptedException - if the current thread is interrupted upon entry, or becomes interrupted while waiting.
    • tryClaim

      public T tryClaim() throws PoolException
      Description copied from interface: PoolTap
      Returns an object from the pool if the pool contains at least one valid object, otherwise returns null. This method will first try to return cached object if available. If no locally cached object is found, it will go through objects in the pool and return the first ready to claim object. If all the objects in the pool is drained, then null will be returned.
      Specified by:
      tryClaim in interface PoolTap<T extends Poolable>
      Returns:
      an object from the pool if the pool contains at least one valid object, otherwise returns null.
      Throws:
      PoolException - If an object allocation failed because the Allocator threw an exception from its allocate method, or returned null, or the expiration check threw an exception.