Package com.zfabrik.util.threading
Class ThreadUtil
- java.lang.Object
-
- com.zfabrik.util.threading.ThreadUtil
-
public class ThreadUtil extends java.lang.Object
Threading utils. Not thread pools and such but rather utilities that help isolating resources and avoiding memory leaks.- Author:
- hb
-
-
Constructor Summary
Constructors Constructor Description ThreadUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> T
cleanContextExceptionExecute(java.lang.ClassLoader contextLoader, java.util.concurrent.Callable<T> callable)
Stupid... but there is places all over that hold on to AccessControlContexts and Context Class Loaders.static <T> T
cleanContextExceptionExecute(java.util.concurrent.Callable<T> callable)
Short version ofcleanContextExceptionExecute(ClassLoader, Callable)
passing innull
as context class loader.static <T> T
cleanContextExecute(java.lang.ClassLoader contextLoader, java.util.concurrent.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.static <T> T
cleanContextExecute(java.util.concurrent.Callable<T> callable)
Short version ofcleanContextExecute(ClassLoader, Callable)
passing innull
as context class loader.static <T,E extends java.lang.Exception>
TcleanContextGet(ThrowingSupplier<T,E> supplier)
Variation ofcleanContextExceptionExecute(Callable)
that relies onThrowingSupplier
to generically limit the amount of noise due to missing exception interference.static <T,E extends java.lang.Exception>
TcleanContextGet(java.lang.ClassLoader contextLoader, ThrowingSupplier<T,E> supplier)
Variation ofcleanContextExceptionExecute(ClassLoader, Callable)
that relies onThrowingSupplier
to generically limit the amount of noise due to missing exception interference.static <E extends java.lang.Exception>
voidcleanContextRun(ThrowingRunnable<E> run)
Short version of#cleanContextRun(ClassLoader, Runnable)
passing innull
as context class loader.static <E extends java.lang.Exception>
voidcleanContextRun(java.lang.ClassLoader contextLoader, ThrowingRunnable<E> run)
Execute aThrowingRunnable
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.static <T> T
cleanContextSupply(java.lang.ClassLoader contextLoader, java.util.function.Supplier<T> supplier)
Execute aSupplier
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.static <T> T
cleanContextSupply(java.util.function.Supplier<T> supplier)
Short version ofcleanContextSupply(ClassLoader, Supplier)
passing innull
as context class loader.static <T,E extends java.lang.Exception>
Tconfine(java.util.concurrent.Callable<T> call, java.lang.Class<E> eclz)
Using this method, an invocation of a callable may be wrapped to only throw a declared checked exception.
-
-
-
Method Detail
-
cleanContextExceptionExecute
public static <T> T cleanContextExceptionExecute(java.lang.ClassLoader contextLoader, java.util.concurrent.Callable<T> callable) throws java.lang.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 loadercallable
- ACallable
to invoke- Returns:
- Return value from the
Callable
invocation - Throws:
java.lang.Exception
-
cleanContextExceptionExecute
public static <T> T cleanContextExceptionExecute(java.util.concurrent.Callable<T> callable) throws java.lang.Exception
Short version ofcleanContextExceptionExecute(ClassLoader, Callable)
passing innull
as context class loader.- Parameters:
callable
- ACallable
to invoke- Returns:
- Return value from the
Callable
invocation - Throws:
java.lang.Exception
-
cleanContextExecute
public static <T> T cleanContextExecute(java.lang.ClassLoader contextLoader, java.util.concurrent.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 loadercallable
- ACallable
to invoke- Returns:
- Return value from the
Callable
invocation
-
cleanContextExecute
public static <T> T cleanContextExecute(java.util.concurrent.Callable<T> callable)
Short version ofcleanContextExecute(ClassLoader, Callable)
passing innull
as context class loader.- Parameters:
callable
- ACallable
to invoke- Returns:
- Return value from the
Callable
invocation
-
cleanContextSupply
public static <T> T cleanContextSupply(java.lang.ClassLoader contextLoader, java.util.function.Supplier<T> supplier)
Execute aSupplier
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 loadersupplier
- ASupplier
to invoke- Returns:
- Return value from the
Supplier
invocation
-
cleanContextSupply
public static <T> T cleanContextSupply(java.util.function.Supplier<T> supplier)
Short version ofcleanContextSupply(ClassLoader, Supplier)
passing innull
as context class loader.- Parameters:
supplier
- ASupplier
to invoke- Returns:
- Return value from the
Supplier
invocation
-
confine
public static <T,E extends java.lang.Exception> T confine(java.util.concurrent.Callable<T> call, java.lang.Class<E> eclz) throws E extends java.lang.Exception
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 aSQLException
, an equivalent execution usingcleanContextExceptionExecute(Callable)
looks like this:confine(()->cleanContextExceptionExecute(this::readTable),SQLException.class);
- Parameters:
eclz
- the confinement exceptioncall
- theCallable
to invoke- Returns:
- the result of the
Callable
invocation - Throws:
E
- A confinement exceptionE extends java.lang.Exception
-
cleanContextGet
public static <T,E extends java.lang.Exception> T cleanContextGet(java.lang.ClassLoader contextLoader, ThrowingSupplier<T,E> supplier) throws E extends java.lang.Exception
Variation ofcleanContextExceptionExecute(ClassLoader, Callable)
that relies onThrowingSupplier
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 java.lang.Exception
-
cleanContextGet
public static <T,E extends java.lang.Exception> T cleanContextGet(ThrowingSupplier<T,E> supplier) throws E extends java.lang.Exception
Variation ofcleanContextExceptionExecute(Callable)
that relies onThrowingSupplier
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 java.lang.Exception
-
cleanContextRun
public static <E extends java.lang.Exception> void cleanContextRun(java.lang.ClassLoader contextLoader, ThrowingRunnable<E> run) throws E extends java.lang.Exception
Execute aThrowingRunnable
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 loaderrun
- ARunnable
to invoke- Throws:
E extends java.lang.Exception
-
cleanContextRun
public static <E extends java.lang.Exception> void cleanContextRun(ThrowingRunnable<E> run) throws E extends java.lang.Exception
Short version of#cleanContextRun(ClassLoader, Runnable)
passing innull
as context class loader.- Parameters:
run
- ARunnable
to invoke- Throws:
E extends java.lang.Exception
-
-