java.lang.Object
org.sentrysoftware.metricshub.engine.common.helpers.ThreadHelper

public class ThreadHelper extends Object
Provides utility methods for thread management, including executing tasks with a specified timeout. This class is part of the MetricsHub Engine's common helper utilities.
  • Constructor Details

    • ThreadHelper

      public ThreadHelper()
  • Method Details

    • execute

      public static <T> T execute(Callable<T> callable, long timeout) throws InterruptedException, ExecutionException, TimeoutException
      Executes a Callable task with a specified timeout. This method creates a single-threaded executor to run the provided task. If the task completes within the given timeout, its result is returned. Otherwise, a TimeoutException is thrown.
      Type Parameters:
      T - the type of the result returned by the callable
      Parameters:
      callable - the task to be executed
      timeout - the maximum time to wait for the task to complete, in seconds
      Returns:
      the result of the executed task
      Throws:
      InterruptedException - if the current thread was interrupted while waiting
      ExecutionException - if the computation threw an exception
      TimeoutException - if the wait timed out