Class ThreadUtils
- java.lang.Object
-
- org.apache.sshd.common.util.threads.ThreadUtils
-
public final class ThreadUtils extends Object
Utility class for thread pools.- Author:
- Apache MINA SSHD Project
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> TcreateDefaultInstance(Class<?> anchor, Class<? extends T> targetType, String className)static <T> TcreateDefaultInstance(ClassLoader cl, Class<? extends T> targetType, String className)static <T> TcreateDefaultInstance(Iterable<? extends ClassLoader> cls, Class<? extends T> targetType, String className)static booleanisInternalThread()Tells whether the calling thread is an Apache MINA sshd framework-internal thread.static Iterator<ClassLoader>iterateDefaultClassLoaders(Class<?> anchor)static CloseableExecutorServicenewCachedThreadPool(String poolName)static CloseableExecutorServicenewCachedThreadPoolIf(CloseableExecutorService executorService, String poolName)static CloseableExecutorServicenewFixedThreadPool(String poolName, int nThreads)static CloseableExecutorServicenewFixedThreadPoolIf(CloseableExecutorService executorService, String poolName, int nThreads)static CloseableExecutorServicenewSingleThreadExecutor(String poolName)static ScheduledExecutorServicenewSingleThreadScheduledExecutor(String poolName)static CloseableExecutorServicenoClose(CloseableExecutorService executorService)static CloseableExecutorServiceprotectExecutorServiceShutdown(CloseableExecutorService executorService, boolean shutdownOnExit)Wraps anCloseableExecutorServicein 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 ClassLoaderresolveDefaultClassLoader(Class<?> anchor)Attempts to find the most suitableClassLoaderas follows:static ClassLoaderresolveDefaultClassLoader(Object anchor)static Iterable<ClassLoader>resolveDefaultClassLoaders(Class<?> anchor)static Iterable<ClassLoader>resolveDefaultClassLoaders(Object anchor)static <V> VrunAsInternal(Callable<V> code)Runs a piece of code given as aCallablewith 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 anIOFunctionwith a flag set indicating that the executing thread is an Apache MINA sshd framework-internal thread.
-
-
-
Method Detail
-
runAsInternal
public static <V> V runAsInternal(Callable<V> code) throws Exception
Runs a piece of code given as aCallablewith 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
public static <T,V> V runAsInternal(T param, IOFunction<? super T,V> code) throws IOExceptionRuns anIOFunctionwith 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:
trueif the thread is considered internal to the framework;falseif not- See Also:
runAsInternal(Callable)
-
protectExecutorServiceShutdown
public static CloseableExecutorService protectExecutorServiceShutdown(CloseableExecutorService executorService, boolean shutdownOnExit)
Wraps anCloseableExecutorServicein 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 theshutdownmethods was invoked.- Parameters:
executorService- The original service - ignored ifnullshutdownOnExit- Iftruethen 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 ClassLoader resolveDefaultClassLoader(Object anchor)
-
resolveDefaultClassLoaders
public static Iterable<ClassLoader> resolveDefaultClassLoaders(Object anchor)
-
resolveDefaultClassLoaders
public static Iterable<ClassLoader> resolveDefaultClassLoaders(Class<?> anchor)
-
resolveDefaultClass
public static Class<?> resolveDefaultClass(Iterable<? extends ClassLoader> cls, String className)
-
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
public static ClassLoader resolveDefaultClassLoader(Class<?> anchor)
Attempts to find the most suitable
ClassLoaderas 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 anchorClassto use if no current thread context class loader - ignored ifnull- Returns:
- The resolved
ClassLoader- Note: might still benullif went all the way "down" to the system class loader and it was alsonull.
-
-
iterateDefaultClassLoaders
public static Iterator<ClassLoader> iterateDefaultClassLoaders(Class<?> anchor)
-
newFixedThreadPoolIf
public static CloseableExecutorService newFixedThreadPoolIf(CloseableExecutorService executorService, String poolName, int nThreads)
-
newFixedThreadPool
public static CloseableExecutorService newFixedThreadPool(String poolName, int nThreads)
-
newCachedThreadPoolIf
public static CloseableExecutorService newCachedThreadPoolIf(CloseableExecutorService executorService, String poolName)
-
newCachedThreadPool
public static CloseableExecutorService newCachedThreadPool(String poolName)
-
newSingleThreadScheduledExecutor
public static ScheduledExecutorService newSingleThreadScheduledExecutor(String poolName)
-
newSingleThreadExecutor
public static CloseableExecutorService newSingleThreadExecutor(String poolName)
-
-