Class ThreadUtils
- java.lang.Object
-
- org.apache.sshd.common.util.threads.ThreadUtils
-
public final class ThreadUtils extends java.lang.Object
Utility class for thread pools.
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.ThreadLocal<java.lang.Boolean>
IS_INTERNAL_THREAD
Marks framework-internal threads.
-
Constructor Summary
Constructors Modifier Constructor Description private
ThreadUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> T
createDefaultInstance(java.lang.Class<?> anchor, java.lang.Class<? extends T> targetType, java.lang.String className)
static <T> T
createDefaultInstance(java.lang.ClassLoader cl, java.lang.Class<? extends T> targetType, java.lang.String className)
static <T> T
createDefaultInstance(java.lang.Iterable<? extends java.lang.ClassLoader> cls, java.lang.Class<? extends T> targetType, java.lang.String className)
static boolean
isInternalThread()
Tells whether the calling thread is an Apache MINA sshd framework-internal thread.static java.util.Iterator<java.lang.ClassLoader>
iterateDefaultClassLoaders(java.lang.Class<?> anchor)
static CloseableExecutorService
newCachedThreadPool(java.lang.String poolName)
static CloseableExecutorService
newCachedThreadPoolIf(CloseableExecutorService executorService, java.lang.String poolName)
static CloseableExecutorService
newFixedThreadPool(java.lang.String poolName, int nThreads)
static CloseableExecutorService
newFixedThreadPoolIf(CloseableExecutorService executorService, java.lang.String poolName, int nThreads)
static CloseableExecutorService
newSingleThreadExecutor(java.lang.String poolName)
static java.util.concurrent.ScheduledExecutorService
newSingleThreadScheduledExecutor(java.lang.String poolName)
static CloseableExecutorService
noClose(CloseableExecutorService executorService)
static CloseableExecutorService
protectExecutorServiceShutdown(CloseableExecutorService executorService, boolean shutdownOnExit)
Wraps anCloseableExecutorService
in such a way as to "protect" it for calls to theExecutorService.shutdown()
orExecutorService.shutdownNow()
.static java.lang.Class<?>
resolveDefaultClass(java.lang.Class<?> anchor, java.lang.String className)
static java.lang.Class<?>
resolveDefaultClass(java.lang.Iterable<? extends java.lang.ClassLoader> cls, java.lang.String className)
static java.lang.ClassLoader
resolveDefaultClassLoader(java.lang.Class<?> anchor)
Attempts to find the most suitableClassLoader
as follows:static java.lang.ClassLoader
resolveDefaultClassLoader(java.lang.Object anchor)
static java.lang.Iterable<java.lang.ClassLoader>
resolveDefaultClassLoaders(java.lang.Class<?> anchor)
static java.lang.Iterable<java.lang.ClassLoader>
resolveDefaultClassLoaders(java.lang.Object anchor)
static <V> V
runAsInternal(java.util.concurrent.Callable<V> code)
Runs a piece of code given as aCallable
with a flag set indicating that the executing thread is an Apache MINA sshd framework-internal thread.static <T,V>
VrunAsInternal(T param, IOFunction<? super T,V> code)
Runs anIOFunction
with a flag set indicating that the executing thread is an Apache MINA sshd framework-internal thread.
-
-
-
Method Detail
-
runAsInternal
public static <V> V runAsInternal(java.util.concurrent.Callable<V> code) throws java.lang.Exception
Runs a piece of code given as aCallable
with a flag set indicating that the executing thread is an Apache MINA sshd framework-internal thread.- Type Parameters:
V
- return type- Parameters:
code
- code to run- Returns:
- the result of
code
- Throws:
java.lang.Exception
- propagated fromcode.call()
- See Also:
isInternalThread()
-
runAsInternal
public static <T,V> V runAsInternal(T param, IOFunction<? super T,V> code) throws java.io.IOException
Runs anIOFunction
with a flag set indicating that the executing thread is an Apache MINA sshd framework-internal thread.- Type Parameters:
T
- parameter typeV
- return type- Parameters:
param
- parameter for the functioncode
- function to run- Returns:
- the result of
code
- Throws:
java.io.IOException
- propagated fromcode.apply()
- See Also:
isInternalThread()
-
isInternalThread
public static boolean isInternalThread()
Tells whether the calling thread is an Apache MINA sshd framework-internal thread.- Returns:
true
if the thread is considered internal to the framework;false
if not- See Also:
runAsInternal(Callable)
-
protectExecutorServiceShutdown
public static CloseableExecutorService protectExecutorServiceShutdown(CloseableExecutorService executorService, boolean shutdownOnExit)
Wraps anCloseableExecutorService
in such a way as to "protect" it for calls to theExecutorService.shutdown()
orExecutorService.shutdownNow()
. All other calls are delegated as-is to the original service. Note: the exposed wrapped proxy will answer correctly theExecutorService.isShutdown()
query if indeed one of theshutdown
methods was invoked.- Parameters:
executorService
- The original service - ignored ifnull
shutdownOnExit
- Iftrue
then it is OK to shutdown the executor so no wrapping takes place.- Returns:
- Either the original service or a wrapped one - depending on the value of the shutdownOnExit parameter
-
noClose
public static CloseableExecutorService noClose(CloseableExecutorService executorService)
-
resolveDefaultClassLoader
public static java.lang.ClassLoader resolveDefaultClassLoader(java.lang.Object anchor)
-
resolveDefaultClassLoaders
public static java.lang.Iterable<java.lang.ClassLoader> resolveDefaultClassLoaders(java.lang.Object anchor)
-
resolveDefaultClassLoaders
public static java.lang.Iterable<java.lang.ClassLoader> resolveDefaultClassLoaders(java.lang.Class<?> anchor)
-
resolveDefaultClass
public static java.lang.Class<?> resolveDefaultClass(java.lang.Class<?> anchor, java.lang.String className)
-
resolveDefaultClass
public static java.lang.Class<?> resolveDefaultClass(java.lang.Iterable<? extends java.lang.ClassLoader> cls, java.lang.String className)
-
createDefaultInstance
public static <T> T createDefaultInstance(java.lang.Class<?> anchor, java.lang.Class<? extends T> targetType, java.lang.String className) throws java.lang.ReflectiveOperationException
- Throws:
java.lang.ReflectiveOperationException
-
createDefaultInstance
public static <T> T createDefaultInstance(java.lang.ClassLoader cl, java.lang.Class<? extends T> targetType, java.lang.String className) throws java.lang.ReflectiveOperationException
- Throws:
java.lang.ReflectiveOperationException
-
createDefaultInstance
public static <T> T createDefaultInstance(java.lang.Iterable<? extends java.lang.ClassLoader> cls, java.lang.Class<? extends T> targetType, java.lang.String className) throws java.lang.ReflectiveOperationException
- Throws:
java.lang.ReflectiveOperationException
-
resolveDefaultClassLoader
public static java.lang.ClassLoader resolveDefaultClassLoader(java.lang.Class<?> anchor)
Attempts to find the most suitable
ClassLoader
as follows:-
Check the
Thread.getContextClassLoader()
value -
If no thread context class loader then check the anchor class (if given) for its class loader
-
If still no loader available, then use
ClassLoader.getSystemClassLoader()
- Parameters:
anchor
- The anchorClass
to use if no current thread context class loader - ignored ifnull
- Returns:
- The resolved
ClassLoader
- Note: might still benull
if went all the way "down" to the system class loader and it was alsonull
.
-
-
iterateDefaultClassLoaders
public static java.util.Iterator<java.lang.ClassLoader> iterateDefaultClassLoaders(java.lang.Class<?> anchor)
-
newFixedThreadPoolIf
public static CloseableExecutorService newFixedThreadPoolIf(CloseableExecutorService executorService, java.lang.String poolName, int nThreads)
-
newFixedThreadPool
public static CloseableExecutorService newFixedThreadPool(java.lang.String poolName, int nThreads)
-
newCachedThreadPoolIf
public static CloseableExecutorService newCachedThreadPoolIf(CloseableExecutorService executorService, java.lang.String poolName)
-
newCachedThreadPool
public static CloseableExecutorService newCachedThreadPool(java.lang.String poolName)
-
newSingleThreadScheduledExecutor
public static java.util.concurrent.ScheduledExecutorService newSingleThreadScheduledExecutor(java.lang.String poolName)
-
newSingleThreadExecutor
public static CloseableExecutorService newSingleThreadExecutor(java.lang.String poolName)
-
-