Class ConcurrentUtils

java.lang.Object
io.github.connellite.concurrent.ConcurrentUtils

public class ConcurrentUtils extends Object
Small helpers for running work with a time limit on a dedicated worker thread.
  • Constructor Details

    • ConcurrentUtils

      public ConcurrentUtils()
  • Method Details

    • runWithTimeout

      public static <T> T runWithTimeout(Callable<T> operation, long timeout, TimeUnit unit) throws Exception
      Runs operation on a worker thread and waits up to timeout in unit.
      Parameters:
      operation - callable to execute; must not be null
      timeout - maximum wait time; must be non-negative
      unit - time unit for timeout; must not be null
      Returns:
      result of operation
      Throws:
      Exception - if operation fails or the wait is interrupted
    • runWithTimeout

      public static <T> T runWithTimeout(Callable<T> operation, long timeout, TimeUnit unit, String operationName) throws Exception
      Runs operation on a worker thread and waits up to timeout in unit.
      Parameters:
      operation - callable to execute; must not be null
      timeout - maximum wait time; must be non-negative
      unit - time unit for timeout; must not be null
      operationName - optional label used in timeout messages; may be null
      Returns:
      result of operation
      Throws:
      Exception - if operation fails or the wait is interrupted
    • runWithTimeout

      public static void runWithTimeout(Runnable operation, long timeout, TimeUnit unit) throws Exception
      Runs operation on a worker thread and waits up to timeout in unit.
      Parameters:
      operation - runnable to execute; must not be null
      timeout - maximum wait time; must be non-negative
      unit - time unit for timeout; must not be null
      Throws:
      Exception - if operation fails or the wait is interrupted
    • runWithTimeout

      public static void runWithTimeout(Runnable operation, long timeout, TimeUnit unit, String operationName) throws Exception
      Runs operation on a worker thread and waits up to timeout in unit.
      Parameters:
      operation - runnable to execute; must not be null
      timeout - maximum wait time; must be non-negative
      unit - time unit for timeout; must not be null
      operationName - optional label used in timeout messages; may be null
      Throws:
      Exception - if operation fails or the wait is interrupted