Package com.zfabrik.util.threading
Class ThreadUtil
java.lang.Object
com.zfabrik.util.threading.ThreadUtil
Threading utils. Not thread pools and such but rather utilities that help
 isolating resources and avoiding memory leaks.
- Author:
- hb
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionstatic <T> TcleanContextExceptionExecute(ClassLoader contextLoader, Callable<T> callable) Stupid...static <T> TcleanContextExceptionExecute(Callable<T> callable) Short version ofcleanContextExceptionExecute(ClassLoader, Callable)passing innullas context class loader.static <T> TcleanContextExecute(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.static <T> TcleanContextExecute(Callable<T> callable) Short version ofcleanContextExecute(ClassLoader, Callable)passing innullas context class loader.static <T,E extends Exception> 
 TcleanContextGet(ThrowingSupplier<T, E> supplier) Variation ofcleanContextExceptionExecute(Callable)that relies onThrowingSupplierto generically limit the amount of noise due to missing exception interference.static <T,E extends Exception> 
 TcleanContextGet(ClassLoader contextLoader, ThrowingSupplier<T, E> supplier) Variation ofcleanContextExceptionExecute(ClassLoader, Callable)that relies onThrowingSupplierto generically limit the amount of noise due to missing exception interference.static <E extends Exception>
 voidcleanContextRun(ThrowingRunnable<E> run) Short version of#cleanContextRun(ClassLoader, Runnable)passing innullas context class loader.static <E extends Exception>
 voidcleanContextRun(ClassLoader contextLoader, ThrowingRunnable<E> run) Execute aThrowingRunnablewith 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> TcleanContextSupply(ClassLoader contextLoader, Supplier<T> supplier) Execute aSupplierwith 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> TcleanContextSupply(Supplier<T> supplier) Short version ofcleanContextSupply(ClassLoader, Supplier)passing innullas context class loader.static <T,E extends Exception> 
 TUsing this method, an invocation of a callable may be wrapped to only throw a declared checked exception.
- 
Constructor Details- 
ThreadUtilpublic ThreadUtil()
 
- 
- 
Method Details- 
cleanContextExceptionExecutepublic 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).
- 
cleanContextExceptionExecuteShort version ofcleanContextExceptionExecute(ClassLoader, Callable)passing innullas context class loader.
- 
cleanContextExecuteExecute 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.
- 
cleanContextExecuteShort version ofcleanContextExecute(ClassLoader, Callable)passing innullas context class loader.
- 
cleanContextSupplyExecute aSupplierwith 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.
- 
cleanContextSupplyShort version ofcleanContextSupply(ClassLoader, Supplier)passing innullas context class loader.
- 
confineUsing 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 readTablethat throws aSQLException, an equivalent execution usingcleanContextExceptionExecute(Callable)looks like this:confine(()->cleanContextExceptionExecute(this::readTable),SQLException.class);
- 
cleanContextGetpublic static <T,E extends Exception> T cleanContextGet(ClassLoader contextLoader, ThrowingSupplier<T, E> supplier) throws EVariation ofcleanContextExceptionExecute(ClassLoader, Callable)that relies onThrowingSupplierto 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
 
- 
cleanContextGetVariation ofcleanContextExceptionExecute(Callable)that relies onThrowingSupplierto 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
 
- 
cleanContextRunpublic static <E extends Exception> void cleanContextRun(ClassLoader contextLoader, ThrowingRunnable<E> run) throws E Execute aThrowingRunnablewith 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.
- 
cleanContextRunShort version of#cleanContextRun(ClassLoader, Runnable)passing innullas context class loader.
 
-