Class ThreadUtil

java.lang.Object
com.zfabrik.util.threading.ThreadUtil

public class ThreadUtil extends Object
Threading utils. Not thread pools and such but rather utilities that help isolating resources and avoiding memory leaks.
Author:
hb
  • Constructor Details

    • ThreadUtil

      public ThreadUtil()
  • Method Details

    • cleanContextExceptionExecute

      public static <T> T cleanContextExceptionExecute(ClassLoader contextLoader, Callable<T> callable) throws Exception
      Stupid... but there is places all over that hold on to AccessControlContexts and Context Class Loaders. This easily consitutes a leak. So, for stuff that doesn't need anything from the context class loader this is a way of keeping context to a minimum;

      Execute a callable with a clean thread context and security context as to avoid any passing on of thread context and security context to another thread that may be spawned from here and may end up holding copies in the end. Any exception thrown will be propagated.

      See also confine(Callable, Class).

      Parameters:
      contextLoader - A class loader to set as context class loader
      callable - A Callable to invoke
      Returns:
      Return value from the Callable invocation
      Throws:
      Exception
    • cleanContextExceptionExecute

      public static <T> T cleanContextExceptionExecute(Callable<T> callable) throws Exception
      Short version of cleanContextExceptionExecute(ClassLoader, Callable) passing in null as context class loader.
      Parameters:
      callable - A Callable to invoke
      Returns:
      Return value from the Callable invocation
      Throws:
      Exception
    • cleanContextExecute

      public static <T> T cleanContextExecute(ClassLoader contextLoader, Callable<T> callable)
      Execute a callable with a clean thread context and security context as to avoid any passing on of thread context and security context to another thread that may be spawned from here and may end up holding copies in the end. Any exception caught will be wrapped in a generic runtime exception.
      Parameters:
      contextLoader - A class loader to set as context class loader
      callable - A Callable to invoke
      Returns:
      Return value from the Callable invocation
    • cleanContextExecute

      public static <T> T cleanContextExecute(Callable<T> callable)
      Short version of cleanContextExecute(ClassLoader, Callable) passing in null as context class loader.
      Parameters:
      callable - A Callable to invoke
      Returns:
      Return value from the Callable invocation
    • cleanContextSupply

      public static <T> T cleanContextSupply(ClassLoader contextLoader, Supplier<T> supplier)
      Execute a Supplier with a clean thread context and security context as to avoid any passing on of thread context and security context to another thread that may be spawned from here and may end up holding copies in the end.
      Parameters:
      contextLoader - A class loader to set as context class loader
      supplier - A Supplier to invoke
      Returns:
      Return value from the Supplier invocation
    • cleanContextSupply

      public static <T> T cleanContextSupply(Supplier<T> supplier)
      Short version of cleanContextSupply(ClassLoader, Supplier) passing in null as context class loader.
      Parameters:
      supplier - A Supplier to invoke
      Returns:
      Return value from the Supplier invocation
    • confine

      public static <T, E extends Exception> T confine(Callable<T> call, Class<E> eclz) throws E
      Using this method, an invocation of a callable may be wrapped to only throw a declared checked exception.

      • If the supplied invocation throws the confinement exception or a sub class, this exception will be rethrown.
      • If the supplied invocation throws the RuntimeException, this exception will be rethrown.
      • Any other exception will be wrapped into a RuntimeException
      • This is handy when it is required to pass a callable given an execution that potentially throws a check exception that is much narrower in scope than simply Exception.

        For example given a method readTable that throws a SQLException, an equivalent execution using cleanContextExceptionExecute(Callable) looks like this:

        
         confine(()->cleanContextExceptionExecute(this::readTable),SQLException.class);
         
      Parameters:
      eclz - the confinement exception
      call - the Callable to invoke
      Returns:
      the result of the Callable invocation
      Throws:
      E - A confinement exception
    • cleanContextGet

      public static <T, E extends Exception> T cleanContextGet(ClassLoader contextLoader, ThrowingSupplier<T,E> supplier) throws E
      Variation of cleanContextExceptionExecute(ClassLoader, Callable) that relies on ThrowingSupplier to generically limit the amount of noise due to missing exception interference. In many places using lambdas this approach is syntactically nicer than using confine
      Throws:
      E extends Exception
    • cleanContextGet

      public static <T, E extends Exception> T cleanContextGet(ThrowingSupplier<T,E> supplier) throws E
      Variation of cleanContextExceptionExecute(Callable) that relies on ThrowingSupplier to generically limit the amount of noise due to missing exception interference. In many places using lambdas this approach is syntactically nicer than using confine
      Throws:
      E extends Exception
    • cleanContextRun

      public static <E extends Exception> void cleanContextRun(ClassLoader contextLoader, ThrowingRunnable<E> run) throws E
      Execute a ThrowingRunnable with a clean thread context and security context as to avoid any passing on of thread context and security context to another thread that may be spawned from here and may end up holding copies in the end.
      Parameters:
      contextLoader - A class loader to set as context class loader
      run - A Runnable to invoke
      Throws:
      E extends Exception
    • cleanContextRun

      public static <E extends Exception> void cleanContextRun(ThrowingRunnable<E> run) throws E
      Short version of #cleanContextRun(ClassLoader, Runnable) passing in null as context class loader.
      Parameters:
      run - A Runnable to invoke
      Throws:
      E extends Exception