Top Description Fields Constructors Methods
org.apache.sshd.common.util.threads

public final Class ThreadUtils

extends Object
Class Inheritance
Imports
java.io.IOException, java.util.Iterator, .NoSuchElementException, java.util.concurrent.Callable, .LinkedBlockingQueue, .ScheduledExecutorService, .ScheduledThreadPoolExecutor, .SynchronousQueue, .ThreadPoolExecutor, .TimeUnit, java.util.function.Supplier, org.apache.sshd.common.util.ReflectionUtils, org.apache.sshd.common.util.io.functors.IOFunction

Utility class for thread pools.
Author
Apache MINA SSHD Project

Field Summary

Modifier and TypeField and Description
private static final ThreadLocal<Boolean>
IS_INTERNAL_THREAD

Marks framework-internal threads.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static <T> T
createDefaultInstance(Class<?> anchor, Class<? extends T> targetType, String className)

public static <T> T
createDefaultInstance(ClassLoader cl, Class<? extends T> targetType, String className)

public static <T> T
createDefaultInstance(Iterable<? extends ClassLoader> cls, Class<? extends T> targetType, String className)

public static boolean

Returns:

true if the thread is considered internal to the framework; false if not
isInternalThread
()

Tells whether the calling thread is an Apache MINA sshd framework-internal thread.

public static Iterator<ClassLoader>
public static CloseableExecutorService
public static CloseableExecutorService
public static CloseableExecutorService
newFixedThreadPool(String poolName, int nThreads)

public static CloseableExecutorService
newFixedThreadPoolIf(CloseableExecutorService executorService, String poolName, int nThreads)

public static CloseableExecutorService
public static ScheduledExecutorService
public static CloseableExecutorService
public static CloseableExecutorService

Returns:

Either the original service or a wrapped one - depending on the value of the shutdownOnExit parameter
protectExecutorServiceShutdown
(CloseableExecutorService
The original service - ignored if null
executorService
,
boolean
If true then it is OK to shutdown the executor so no wrapping takes place.
shutdownOnExit
)

Wraps an CloseableExecutorService in such a way as to "protect" it for calls to the CloseableExecutorService#shutdown() or CloseableExecutorService#shutdownNow().

public static Class<?>
resolveDefaultClass(Class<?> anchor, String className)

public static Class<?>
resolveDefaultClass(Iterable<? extends ClassLoader> cls, String className)

public static ClassLoader
public static ClassLoader

Returns:

The resolved ClassLoader - Note: might still be null if went all the way "down" to the system class loader and it was also null.
resolveDefaultClassLoader
(Class<?>
The anchor Class to use if no current thread context class loader - ignored if null
anchor
)

Attempts to find the most suitable ClassLoader as follows:

public static Iterable<ClassLoader>
public static Iterable<ClassLoader>
public static <
return type
V
>
V

Returns:

the result of code
runAsInternal
(Callable<V>
code to run
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.

public static <
parameter type
T
,
return type
V
>
V

Returns:

the result of code
runAsInternal
(T
parameter for the function
param
,
IOFunction<? super T, V>
function to run
code
)

Runs an IOFunction with a flag set indicating that the executing thread is an Apache MINA sshd framework-internal thread.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

IS_INTERNAL_THREADback to summary
private static final ThreadLocal<Boolean> IS_INTERNAL_THREAD

Marks framework-internal threads.

Constructor Detail

ThreadUtilsback to summary
private ThreadUtils()

Method Detail

createDefaultInstanceback to summary
public static <T> T createDefaultInstance(Class<?> anchor, Class<? extends T> targetType, String className) throws ReflectiveOperationException
createDefaultInstanceback to summary
public static <T> T createDefaultInstance(ClassLoader cl, Class<? extends T> targetType, String className) throws ReflectiveOperationException
createDefaultInstanceback to summary
public static <T> T createDefaultInstance(Iterable<? extends ClassLoader> cls, Class<? extends T> targetType, String className) throws ReflectiveOperationException
isInternalThreadback to summary
public static boolean isInternalThread()

Tells whether the calling thread is an Apache MINA sshd framework-internal thread.

Returns:boolean

true if the thread is considered internal to the framework; false if not

See Also
runAsInternal(Callable)
iterateDefaultClassLoadersback to summary
public static Iterator<ClassLoader> iterateDefaultClassLoaders(Class<?> anchor)
newCachedThreadPoolback to summary
public static CloseableExecutorService newCachedThreadPool(String poolName)
newCachedThreadPoolIfback to summary
public static CloseableExecutorService newCachedThreadPoolIf(CloseableExecutorService executorService, String poolName)
newFixedThreadPoolback to summary
public static CloseableExecutorService newFixedThreadPool(String poolName, int nThreads)
newFixedThreadPoolIfback to summary
public static CloseableExecutorService newFixedThreadPoolIf(CloseableExecutorService executorService, String poolName, int nThreads)
newSingleThreadExecutorback to summary
public static CloseableExecutorService newSingleThreadExecutor(String poolName)
newSingleThreadScheduledExecutorback to summary
public static ScheduledExecutorService newSingleThreadScheduledExecutor(String poolName)
noCloseback to summary
public static CloseableExecutorService noClose(CloseableExecutorService executorService)
protectExecutorServiceShutdownback to summary
public static CloseableExecutorService protectExecutorServiceShutdown(CloseableExecutorService executorService, boolean shutdownOnExit)

Wraps an CloseableExecutorService in such a way as to "protect" it for calls to the CloseableExecutorService#shutdown() or CloseableExecutorService#shutdownNow(). All other calls are delegated as-is to the original service

Note

the exposed wrapped proxy will answer correctly the CloseableExecutorService#isShutdown() query if indeed one of the shutdown methods was invoked.

Parameters
executorService:CloseableExecutorService

The original service - ignored if null

shutdownOnExit:boolean

If true then it is OK to shutdown the executor so no wrapping takes place.

Returns:CloseableExecutorService

Either the original service or a wrapped one - depending on the value of the shutdownOnExit parameter

resolveDefaultClassback to summary
public static Class<?> resolveDefaultClass(Class<?> anchor, String className)
resolveDefaultClassback to summary
public static Class<?> resolveDefaultClass(Iterable<? extends ClassLoader> cls, String className)
resolveDefaultClassLoaderback to summary
public static ClassLoader resolveDefaultClassLoader(Object anchor)
resolveDefaultClassLoaderback to summary
public static ClassLoader resolveDefaultClassLoader(Class<?> anchor)

Attempts to find the most suitable ClassLoader as follows:

Parameters
anchor:Class<?>

The anchor Class to use if no current thread context class loader - ignored if null

Returns:ClassLoader

The resolved ClassLoader - Note: might still be null if went all the way "down" to the system class loader and it was also null.

resolveDefaultClassLoadersback to summary
public static Iterable<ClassLoader> resolveDefaultClassLoaders(Object anchor)
resolveDefaultClassLoadersback to summary
public static Iterable<ClassLoader> resolveDefaultClassLoaders(Class<?> anchor)
runAsInternalback to summary
public static <V> V runAsInternal(Callable<V> code) throws Exception

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.

Parameters
<V>
return type
code:Callable<V>

code to run

Returns:V

the result of code

Exceptions
Exception:
propagated from code.call()
See Also
isInternalThread()
runAsInternalback to summary
public static <T, V> V runAsInternal(T param, IOFunction<? super T, V> code) throws IOException

Runs an IOFunction with a flag set indicating that the executing thread is an Apache MINA sshd framework-internal thread.

Parameters
<T>
parameter type
<V>
return type
param:T

parameter for the function

code:IOFunction<? super T, V>

function to run

Returns:V

the result of code

Exceptions
IOException:
propagated from code.apply()
See Also
isInternalThread()