Class ThreadUtils
java.lang.Object
org.apache.sshd.common.util.threads.ThreadUtils
Utility class for thread pools.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final ThreadLocal
<Boolean> Marks framework-internal threads. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T
createDefaultInstance
(Class<?> anchor, Class<? extends T> targetType, String className) static <T> T
createDefaultInstance
(ClassLoader cl, Class<? extends T> targetType, String className) static <T> T
createDefaultInstance
(Iterable<? extends ClassLoader> cls, Class<? extends T> targetType, String className) static boolean
Tells whether the calling thread is an Apache MINA sshd framework-internal thread.static Iterator
<ClassLoader> iterateDefaultClassLoaders
(Class<?> anchor) static CloseableExecutorService
newCachedThreadPool
(String poolName) static CloseableExecutorService
newCachedThreadPoolIf
(CloseableExecutorService executorService, String poolName) static CloseableExecutorService
newFixedThreadPool
(String poolName, int nThreads) static CloseableExecutorService
newFixedThreadPoolIf
(CloseableExecutorService executorService, String poolName, int nThreads) static CloseableExecutorService
newSingleThreadExecutor
(String poolName) static ScheduledExecutorService
newSingleThreadScheduledExecutor
(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 Class
<?> resolveDefaultClass
(Class<?> anchor, String className) static Class
<?> resolveDefaultClass
(Iterable<? extends ClassLoader> cls, String className) static ClassLoader
resolveDefaultClassLoader
(Class<?> anchor) Attempts to find the most suitableClassLoader
as follows:static ClassLoader
resolveDefaultClassLoader
(Object anchor) static Iterable
<ClassLoader> resolveDefaultClassLoaders
(Class<?> anchor) static Iterable
<ClassLoader> resolveDefaultClassLoaders
(Object anchor) static <V> V
runAsInternal
(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> V runAsInternal
(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.
-
Field Details
-
IS_INTERNAL_THREAD
Marks framework-internal threads.
-
-
Constructor Details
-
ThreadUtils
private ThreadUtils()
-
-
Method Details
-
runAsInternal
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:
Exception
- propagated fromcode.call()
-
runAsInternal
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:
Exception
- propagated fromcode.apply()
IOException
-
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:
-
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
-
resolveDefaultClassLoader
-
resolveDefaultClassLoaders
-
resolveDefaultClassLoaders
-
resolveDefaultClass
-
resolveDefaultClass
-
createDefaultInstance
public static <T> T createDefaultInstance(Class<?> anchor, Class<? extends T> targetType, String className) throws ReflectiveOperationException - Throws:
ReflectiveOperationException
-
createDefaultInstance
public static <T> T createDefaultInstance(ClassLoader cl, Class<? extends T> targetType, String className) throws ReflectiveOperationException - Throws:
ReflectiveOperationException
-
createDefaultInstance
public static <T> T createDefaultInstance(Iterable<? extends ClassLoader> cls, Class<? extends T> targetType, String className) throws ReflectiveOperationException - Throws:
ReflectiveOperationException
-
resolveDefaultClassLoader
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
-
newFixedThreadPoolIf
public static CloseableExecutorService newFixedThreadPoolIf(CloseableExecutorService executorService, String poolName, int nThreads) -
newFixedThreadPool
-
newCachedThreadPoolIf
public static CloseableExecutorService newCachedThreadPoolIf(CloseableExecutorService executorService, String poolName) -
newCachedThreadPool
-
newSingleThreadScheduledExecutor
-
newSingleThreadExecutor
-