public final class ThreadUtils extends Object
Modifier and Type | Method and Description |
---|---|
static <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 |
isInternalThread()
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 an
CloseableExecutorService in such a way as to "protect" it for calls to the
ExecutorService.shutdown() or ExecutorService.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 suitable
ClassLoader 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 a
Callable 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 an
IOFunction with a flag set indicating that the executing thread is an Apache MINA sshd
framework-internal thread. |
public static <V> V runAsInternal(Callable<V> code) throws Exception
Callable
with a flag set indicating that the executing thread is an
Apache MINA sshd framework-internal thread.V
- return typecode
- code to runcode
Exception
- propagated from code.call()
#isInternal()}
public static <T,V> V runAsInternal(T param, IOFunction<? super T,V> code) throws IOException
IOFunction
with a flag set indicating that the executing thread is an Apache MINA sshd
framework-internal thread.T
- parameter typeV
- return typeparam
- parameter for the functioncode
- function to runcode
Exception
- propagated from code.apply()
IOException
#isInternal()}
public static boolean isInternalThread()
true
if the thread is considered internal to the framework; false
if notrunAsInternal(Callable)
public static CloseableExecutorService protectExecutorServiceShutdown(CloseableExecutorService executorService, boolean shutdownOnExit)
CloseableExecutorService
in such a way as to "protect" it for calls to the
ExecutorService.shutdown()
or ExecutorService.shutdownNow()
. All other calls
are delegated as-is to the original service. Note: the exposed wrapped proxy will answer correctly the
ExecutorService.isShutdown()
query if indeed one of the shutdown
methods was invoked.executorService
- The original service - ignored if null
shutdownOnExit
- If true
then it is OK to shutdown the executor so no wrapping takes place.public static CloseableExecutorService noClose(CloseableExecutorService executorService)
public static ClassLoader resolveDefaultClassLoader(Object anchor)
public static Iterable<ClassLoader> resolveDefaultClassLoaders(Object anchor)
public static Iterable<ClassLoader> resolveDefaultClassLoaders(Class<?> anchor)
public static Class<?> resolveDefaultClass(Iterable<? extends ClassLoader> cls, String className)
public static <T> T createDefaultInstance(Class<?> anchor, Class<? extends T> targetType, String className) throws ReflectiveOperationException
ReflectiveOperationException
public static <T> T createDefaultInstance(ClassLoader cl, Class<? extends T> targetType, String className) throws ReflectiveOperationException
ReflectiveOperationException
public static <T> T createDefaultInstance(Iterable<? extends ClassLoader> cls, Class<? extends T> targetType, String className) throws ReflectiveOperationException
ReflectiveOperationException
public static ClassLoader resolveDefaultClassLoader(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()
anchor
- The anchor Class
to use if no current thread context class loader - ignored if
null
ClassLoader
- Note: might still be null
if went all the way
"down" to the system class loader and it was also null
.public static Iterator<ClassLoader> iterateDefaultClassLoaders(Class<?> anchor)
public static CloseableExecutorService newFixedThreadPoolIf(CloseableExecutorService executorService, String poolName, int nThreads)
public static CloseableExecutorService newFixedThreadPool(String poolName, int nThreads)
public static CloseableExecutorService newCachedThreadPoolIf(CloseableExecutorService executorService, String poolName)
public static CloseableExecutorService newCachedThreadPool(String poolName)
public static ScheduledExecutorService newSingleThreadScheduledExecutor(String poolName)
public static CloseableExecutorService newSingleThreadExecutor(String poolName)
Copyright © 2008–2024 The Apache Software Foundation. All rights reserved.