saker.build Documentation TaskDoc JavaDoc Packages
public class ThreadUtils
Utility class containing functions and classes related to threads and their manipulation.

The class contains support for thread pools, running multiple worker functions concurrently, dealing with threads, and others.

Nested types
public interface
Interface provided to task runner implementation which can signal cancellation of the operation.
public static final class
Builder class for executing various tasks with the specified configuration concurrently.
public interface
Interface for a thread pool that is capable of executing tasks offered to it.
public interface
Functional interface similar to Consumer, but is capable of throwing an arbitrary exception.
public interface
Functional interface similar to ThrowingConsumer that can take an another context argument for its execution.
Methods
public static int
Writes the stack traces of all threads in the JVM to the given print stream.
public static int
Writes the stack traces of all threads in the JVM that the predicate allows.
public static int
Writes the stack traces of all threads in the JVM which have the given thread group as any of its parent.
public static int
Not recommended to use, keep track of your desired thread factor, and pass it to the appropriate configuration parameters instead of using an inheritable thread local configuration.
public static ThreadGroup
Gets the root (top-level) thread group.
public static boolean
Checks if the given thread has the argument thread group as any of its parent.
public static void
Interrupts the argument thread if non-null.
public static void
joinThreads(Iterable<extends Thread> threads)
Joins the argument threads.
public static void
Joins the argument thread.
public static void
joinThreads(Thread... threads)
Joins the argument threads.
public static void
Joins the argument threads non-interruptibly.
public static void
Joins the argument thread non-interruptibly.
public static void
Joins the argument threads non-interruptibly.
public static ThreadWorkPool
Gets a work pool that executes any task offered to it on the caller thread, at the moment it was offered.
public static ThreadWorkPool
Creates a new work pool that dynamically creates new threads when new tasks are posted.
public static ThreadWorkPool
Creates a new work pool that dynamically creates new threads when new tasks are posted.
public static ThreadWorkPool
Creates a new work pool that dynamically creates new threads when new tasks are posted.
public static ThreadWorkPool
Creates a new work pool that dynamically creates new threads when new tasks are posted.
public static ThreadWorkPool
Creates a new work pool that dynamically creates new threads when new tasks are posted.
public static ThreadWorkPool
newDynamicWorkPool(ThreadGroup group, String nameprefix, OperationCancelMonitor monitor, boolean daemon)
Creates a new work pool that dynamically creates new threads when new tasks are posted.
public static ThreadWorkPool
Creates a new work pool that dynamically creates new threads when new tasks are posted.
public static Lock
Creates a new non-reentrant Lock that can only be exclusively held by a single thread.
public static ThreadWorkPool
Creates a new work pool that uses a fixed number of threads for task execution.
public static ThreadWorkPool
newFixedWorkPool(int threadCount)
Creates a new work pool that uses a fixed number of threads for task execution.
public static ThreadWorkPool
newFixedWorkPool(int threadCount, String nameprefix)
Creates a new work pool that uses a fixed number of threads for task execution.
public static ThreadWorkPool
newFixedWorkPool(int threadCount, String nameprefix, boolean daemon)
Creates a new work pool that uses a fixed number of threads for task execution.
public static ThreadWorkPool
newFixedWorkPool(int threadCount, OperationCancelMonitor monitor)
Creates a new work pool that uses a fixed number of threads for task execution.
public static ThreadWorkPool
newFixedWorkPool(int threadCount, OperationCancelMonitor monitor, String nameprefix)
Creates a new work pool that uses a fixed number of threads for task execution.
public static ThreadWorkPool
Creates a new work pool that uses a fixed number of threads for task execution.
public static ThreadWorkPool
Creates a new work pool that uses a fixed number of threads for task execution.
public static ThreadWorkPool
newFixedWorkPool(ThreadGroup group, int threadCount)
Creates a new work pool that uses a fixed number of threads for task execution.
public static ThreadWorkPool
newFixedWorkPool(ThreadGroup group, int threadCount, String nameprefix, boolean daemon)
Creates a new work pool that uses a fixed number of threads for task execution.
public static ThreadWorkPool
newFixedWorkPool(ThreadGroup group, int threadCount, OperationCancelMonitor monitor)
Creates a new work pool that uses a fixed number of threads for task execution.
public static ThreadWorkPool
newFixedWorkPool(ThreadGroup group, int threadCount, OperationCancelMonitor monitor, String nameprefix)
Creates a new work pool that uses a fixed number of threads for task execution.
public static ThreadWorkPool
newFixedWorkPool(ThreadGroup group, int threadCount, OperationCancelMonitor monitor, String nameprefix, boolean daemon)
Creates a new work pool that uses a fixed number of threads for task execution.
public static ThreadWorkPool
Creates a new work pool that uses a fixed number of threads for task execution.
public static ThreadWorkPool
newFixedWorkPool(ThreadFactory threadfactory, int threadCount, OperationCancelMonitor monitor)
Creates a new work pool that uses a fixed number of threads for task execution.
public static ParallelRunner
Creates a new parallel runner builder.
public static <T, C> void
runParallelContextItems(Iterable<extends T> items, Supplier<extends C> contextsupplier, ThrowingContextConsumer<super T, ? super C> worker)
Runs the given worker for the specified items and context object supplier the concurrently.
public static <T> void
runParallelItems(Iterable<extends T> items, ThrowingConsumer<super T> worker)
Runs the given worker for the specified items concurrently.
public static void
runParallelRunnables(Iterable<extends Runnable> runnables)
Runs the argument runnables concurrently.
public static void
Runs the argument runnables concurrently.
public static void
This method doesn't do anything anymore. Not recommended to use, keep track of your desired thread factor, and pass it to the appropriate configuration parameters instead of using an inheritable thread local configuration.
public static Thread
Starts a new daemon thread with the given name that executes the argument runnable.
public static Thread
Starts a new daemon thread with the given name that executes the argument runnable.
public static Thread
Starts a new daemon thread on the given thread group that executes the argument runnable.
public static Thread
Starts a new daemon thread on the given thread group with the specified name that executes the argument runnable.
public static Thread
Starts a new thread that executes the argument runnable.
public static Thread
startThread(String name, Runnable runnable)
Starts a new thread with the given name that executes the argument runnable.
public static Thread
Starts a new thread on the given thread group that executes the argument runnable.
public static Thread
startThread(ThreadGroup group, String name, Runnable runnable)
Starts a new thread on the given thread group with the specified name that executes the argument runnable.
Writes the stack traces of all threads in the JVM to the given print stream.

This method should be used only for informational purposes.

psThe print stream to write the stack traces to.
The number of threads that have been written to the output.
NullPointerExceptionIf the print stream is null.
public static int dumpAllThreadStackTraces(PrintStream ps, Predicate<super Thread> predicate) throws NullPointerException
Writes the stack traces of all threads in the JVM that the predicate allows.

This method should be used only for informational purposes.

psThe print stream to write the stack traces to.
predicateThe predicate to test whether a thread stack traces should be written.
The number of threads that have been written to the output.
NullPointerExceptionIf the print stream is null.
public static int dumpThreadGroupStackTraces(PrintStream ps, ThreadGroup threadgroup) throws NullPointerException
Writes the stack traces of all threads in the JVM which have the given thread group as any of its parent.

This method should be used only for informational purposes.

psThe print stream to write the stack traces to.
threadgroupThe thread group for which to list the threads.
The number of threads that have been written to the output.
NullPointerExceptionIf the print stream is null.
Not recommended to use, keep track of your desired thread factor, and pass it to the appropriate configuration parameters instead of using an inheritable thread local configuration.
Gets the default thread concurrency factor.

This method used to return the value of an inheritable thread local, with an initial value of Math.max(Runtime.getRuntime().availableProcessors() * 3 / 2, 2). Now it returns this fixed initial value.

The thread factor.
saker.util 0.8.2
Gets the root (top-level) thread group.

The top-level thread group is the one which has no parent.

The top-level thread group;
public static boolean hasParentThreadGroup(Thread t, ThreadGroup group)
Checks if the given thread has the argument thread group as any of its parent.

If any of the arguments are null, false is returned.

tThe thread.
groupThe thread group.
true if the thread exists in the hierarchy of the given thread group.
public static void interruptThread(Thread t)
Interrupts the argument thread if non-null.
tThe thread to interrupt.
public static void joinThreads(Iterable<extends Thread> threads) throws InterruptedException, IllegalThreadStateException
Joins the argument threads.

If this method finishes successfully, all the arguments thread will be in a finished state.

threadsThe threads to join. May be null, in which case this function call is a no-op.
InterruptedExceptionIf the current thread was interrupted while joining.
IllegalThreadStateExceptionIf the argument contains the current thread. (I.e. the thread tries to join itself)
Joins the argument thread.

This is a single Thread parameter overload of joinThreads(Thread...).

threadThe thread to join. May be null, in which case this function call is a no-op.
InterruptedExceptionIf the current thread was interrupted while joining.
IllegalThreadStateExceptionIf the argument is the current thread. (I.e. the thread tries to join itself)
saker.util 0.8.4
public static void joinThreads(Thread... threads) throws InterruptedException, IllegalThreadStateException
Joins the argument threads.

If this method finishes successfully, all the arguments thread will be in a finished state.

threadsThe threads to join. May be null, in which case this function call is a no-op.
InterruptedExceptionIf the current thread was interrupted while joining.
IllegalThreadStateExceptionIf the argument contains the current thread. (I.e. the thread tries to join itself)
public static void joinThreadsNonInterruptible(Iterable<extends Thread> threads) throws IllegalThreadStateException
Joins the argument threads non-interruptibly.

If the current thread is interrupted while joining, the interrupt flag is stored, and the joining will continue. If the thread was interrupted, it will be reinterrupted at the end of the method.

If this method finishes successfully, all the arguments thread will be in a finished state.

threadsThe threads to join. May be null, in which case this function call is a no-op.
IllegalThreadStateExceptionIf the argument contains the current thread. (I.e. the thread tries to join itself)
Joins the argument thread non-interruptibly.

If the current thread is interrupted while joining, the interrupt flag is stored, and the joining will continue. If the thread was interrupted, it will be reinterrupted at the end of the method.

This is a single Thread parameter overload of joinThreadsNonInterruptible(Thread...).

threadThe thread to join. May be null, in which case this function call is a no-op.
IllegalThreadStateExceptionIf the argument is the current thread. (I.e. the thread tries to join itself)
saker.util 0.8.4
public static void joinThreadsNonInterruptible(Thread... threads) throws IllegalThreadStateException
Joins the argument threads non-interruptibly.

If the current thread is interrupted while joining, the interrupt flag is stored, and the joining will continue. If the thread was interrupted, it will be reinterrupted at the end of the method.

If this method finishes successfully, all the arguments thread will be in a finished state.

threadsThe threads to join. May be null, in which case this function call is a no-op.
IllegalThreadStateExceptionIf the argument contains the current thread. (I.e. the thread tries to join itself)
Gets a work pool that executes any task offered to it on the caller thread, at the moment it was offered.

The returned work pool will execute offered task immediately when called.

The work pool.
Creates a new work pool that dynamically creates new threads when new tasks are posted.

The returned work pool will cache threads for some time, and dynamically allocate new ones if necessary. It will also exit threads if they've not been used for some time.

The created work pool.
public static ThreadWorkPool newDynamicWorkPool(String nameprefix)
Creates a new work pool that dynamically creates new threads when new tasks are posted.

The returned work pool will cache threads for some time, and dynamically allocate new ones if necessary. It will also exit threads if they've not been used for some time.

nameprefixThe name prefix to use for each created thread. Recommended format is "Something-". A thread number identifier will be appended to it by the work pool.
The created work pool.
Creates a new work pool that dynamically creates new threads when new tasks are posted.

The returned work pool will cache threads for some time, and dynamically allocate new ones if necessary. It will also exit threads if they've not been used for some time.

groupThe thread group for the created threads. All created threads will be instantiated with the given thread group as its parent.
The created work pool.
public static ThreadWorkPool newDynamicWorkPool(ThreadGroup group, String nameprefix)
Creates a new work pool that dynamically creates new threads when new tasks are posted.

The returned work pool will cache threads for some time, and dynamically allocate new ones if necessary. It will also exit threads if they've not been used for some time.

groupThe thread group for the created threads. All created threads will be instantiated with the given thread group as its parent.
nameprefixThe name prefix to use for each created thread. Recommended format is "Something-". A thread number identifier will be appended to it by the work pool.
The created work pool.
Creates a new work pool that dynamically creates new threads when new tasks are posted.

The returned work pool will cache threads for some time, and dynamically allocate new ones if necessary. It will also exit threads if they've not been used for some time.

groupThe thread group for the created threads. All created threads will be instantiated with the given thread group as its parent.
nameprefixThe name prefix to use for each created thread. Recommended format is "Something-". A thread number identifier will be appended to it by the work pool.
monitorCancellation monitor for the tasks. May be null.
The created work pool.
public static ThreadWorkPool newDynamicWorkPool(ThreadGroup group, String nameprefix, OperationCancelMonitor monitor, boolean daemon)
Creates a new work pool that dynamically creates new threads when new tasks are posted.

The returned work pool will cache threads for some time, and dynamically allocate new ones if necessary. It will also exit threads if they've not been used for some time.

groupThe thread group for the created threads. All created threads will be instantiated with the given thread group as its parent.
nameprefixThe name prefix to use for each created thread. Recommended format is "Something-". A thread number identifier will be appended to it by the work pool.
monitorCancellation monitor for the tasks. May be null.
daemonBoolean indicating whether the created threads in the work pool should be daemon.
The created work pool.
Creates a new work pool that dynamically creates new threads when new tasks are posted.

The returned work pool will cache threads for some time, and dynamically allocate new ones if necessary. It will also exit threads if they've not been used for some time.

threadfactoryThe thread factory to use for creating new threads. May be null in which case the new threads are created and their daemon flag and thread group is set based on the thread that creates the pool.
monitorCancellation monitor for the tasks. May be null.
The created work pool.
saker.util 0.8.4
public static Lock newExclusiveLock()
Creates a new non-reentrant Lock that can only be exclusively held by a single thread.

The lock will throw an IllegalThreadStateException in case reentrant locking is attempted.

The new exclusive lock.
saker.util 0.8.4
Creates a new work pool that uses a fixed number of threads for task execution.

The threads are lazily started when new tasks are offered to the thread pool.

The work pool.
public static ThreadWorkPool newFixedWorkPool(int threadCount)
Creates a new work pool that uses a fixed number of threads for task execution.

The threads are lazily started when new tasks are offered to the thread pool.

threadCountThe maximum number of threads that the thread pool spawns. Negative or 0 means the default value.
The work pool.
public static ThreadWorkPool newFixedWorkPool(int threadCount, String nameprefix)
Creates a new work pool that uses a fixed number of threads for task execution.

The threads are lazily started when new tasks are offered to the thread pool.

threadCountThe maximum number of threads that the thread pool spawns. Negative or 0 means the default value.
nameprefixThe name prefix to use for each created thread. Recommended format is "Something-". A thread number identifier will be appended to it by the work pool.
The work pool.
public static ThreadWorkPool newFixedWorkPool(int threadCount, String nameprefix, boolean daemon)
Creates a new work pool that uses a fixed number of threads for task execution.

The threads are lazily started when new tasks are offered to the thread pool.

threadCountThe maximum number of threads that the thread pool spawns. Negative or 0 means the default value.
nameprefixThe name prefix to use for each created thread. Recommended format is "Something-". A thread number identifier will be appended to it by the work pool.
daemonBoolean indicating whether the created threads in the work pool should be daemon.
The work pool.
public static ThreadWorkPool newFixedWorkPool(int threadCount, OperationCancelMonitor monitor)
Creates a new work pool that uses a fixed number of threads for task execution.

The threads are lazily started when new tasks are offered to the thread pool.

threadCountThe maximum number of threads that the thread pool spawns. Negative or 0 means the default value.
monitorCancellation monitor for the tasks. May be null.
The work pool.
public static ThreadWorkPool newFixedWorkPool(int threadCount, OperationCancelMonitor monitor, String nameprefix)
Creates a new work pool that uses a fixed number of threads for task execution.

The threads are lazily started when new tasks are offered to the thread pool.

threadCountThe maximum number of threads that the thread pool spawns. Negative or 0 means the default value.
monitorCancellation monitor for the tasks. May be null.
nameprefixThe name prefix to use for each created thread. Recommended format is "Something-". A thread number identifier will be appended to it by the work pool.
The work pool.
public static ThreadWorkPool newFixedWorkPool(String nameprefix)
Creates a new work pool that uses a fixed number of threads for task execution.

The threads are lazily started when new tasks are offered to the thread pool.

nameprefixThe name prefix to use for each created thread. Recommended format is "Something-". A thread number identifier will be appended to it by the work pool.
The work pool.
Creates a new work pool that uses a fixed number of threads for task execution.

The threads are lazily started when new tasks are offered to the thread pool.

groupThe thread group for the created threads. All created threads will be instantiated with the given thread group as its parent.
The work pool.
public static ThreadWorkPool newFixedWorkPool(ThreadGroup group, int threadCount)
Creates a new work pool that uses a fixed number of threads for task execution.

The threads are lazily started when new tasks are offered to the thread pool.

groupThe thread group for the created threads. All created threads will be instantiated with the given thread group as its parent.
threadCountThe maximum number of threads that the thread pool spawns. Negative or 0 means the default value.
The work pool.
public static ThreadWorkPool newFixedWorkPool(ThreadGroup group, int threadCount, String nameprefix, boolean daemon)
Creates a new work pool that uses a fixed number of threads for task execution.

The threads are lazily started when new tasks are offered to the thread pool.

groupThe thread group for the created threads. All created threads will be instantiated with the given thread group as its parent.
threadCountThe maximum number of threads that the thread pool spawns. Negative or 0 means the default value.
nameprefixThe name prefix to use for each created thread. Recommended format is "Something-". A thread number identifier will be appended to it by the work pool.
daemonBoolean indicating whether the created threads in the work pool should be daemon.
The work pool.
public static ThreadWorkPool newFixedWorkPool(ThreadGroup group, int threadCount, OperationCancelMonitor monitor)
Creates a new work pool that uses a fixed number of threads for task execution.

The threads are lazily started when new tasks are offered to the thread pool.

groupThe thread group for the created threads. All created threads will be instantiated with the given thread group as its parent.
threadCountThe maximum number of threads that the thread pool spawns. Negative or 0 means the default value.
monitorCancellation monitor for the tasks. May be null.
The work pool.
public static ThreadWorkPool newFixedWorkPool(ThreadGroup group, int threadCount, OperationCancelMonitor monitor, String nameprefix)
Creates a new work pool that uses a fixed number of threads for task execution.

The threads are lazily started when new tasks are offered to the thread pool.

groupThe thread group for the created threads. All created threads will be instantiated with the given thread group as its parent.
threadCountThe maximum number of threads that the thread pool spawns. Negative or 0 means the default value.
monitorCancellation monitor for the tasks. May be null.
nameprefixThe name prefix to use for each created thread. Recommended format is "Something-". A thread number identifier will be appended to it by the work pool.
The work pool.
public static ThreadWorkPool newFixedWorkPool(ThreadGroup group, int threadCount, OperationCancelMonitor monitor, String nameprefix, boolean daemon)
Creates a new work pool that uses a fixed number of threads for task execution.

The threads are lazily started when new tasks are offered to the thread pool.

groupThe thread group for the created threads. All created threads will be instantiated with the given thread group as its parent.
threadCountThe maximum number of threads that the thread pool spawns. Negative or 0 means the default value.
monitorCancellation monitor for the tasks. May be null.
nameprefixThe name prefix to use for each created thread. Recommended format is "Something-". A thread number identifier will be appended to it by the work pool.
daemonBoolean indicating whether the created threads in the work pool should be daemon.
The work pool.
public static ThreadWorkPool newFixedWorkPool(ThreadGroup group, String nameprefix)
Creates a new work pool that uses a fixed number of threads for task execution.

The threads are lazily started when new tasks are offered to the thread pool.

groupThe thread group for the created threads. All created threads will be instantiated with the given thread group as its parent.
nameprefixThe name prefix to use for each created thread. Recommended format is "Something-". A thread number identifier will be appended to it by the work pool.
The work pool.
public static ThreadWorkPool newFixedWorkPool(ThreadFactory threadfactory, int threadCount, OperationCancelMonitor monitor)
Creates a new work pool that uses a fixed number of threads for task execution.

The threads are lazily started when new tasks are offered to the thread pool.

threadfactoryThe thread factory to use for creating new threads. May be null in which case the new threads are created and their daemon flag and thread group is set based on the thread that creates the pool.
threadCountThe maximum number of threads that the thread pool spawns. Negative or 0 means the default value.
monitorCancellation monitor for the tasks. May be null.
The work pool.
saker.util 0.8.4
Creates a new parallel runner builder.

Parallel runners can be used to run specific tasks concurrently, without dealing with thread pools.

See ParallelRunner for more information.

The parallel runner.
public static <T, C> void runParallelContextItems(Iterable<extends T> items, Supplier<extends C> contextsupplier, ThrowingContextConsumer<super T, ? super C> worker)
Runs the given worker for the specified items and context object supplier the concurrently.

See ParallelRunner for more information.

TThe type of the items.
CThe type of the context object.
itemsThe items to run the worker concurrently.
contextsupplierThe context supplier to create the context object for the workers.
workerThe worker that executes for each item.
public static <T> void runParallelItems(Iterable<extends T> items, ThrowingConsumer<super T> worker)
Runs the given worker for the specified items concurrently.

See ParallelRunner for more information.

TThe type of the items.
itemsThe items to run the worker concurrently.
workerThe worker to execute for each item.
public static void runParallelRunnables(Iterable<extends Runnable> runnables)
Runs the argument runnables concurrently.

See ParallelRunner for more information.

runnablesThe runnables.
public static void runParallelRunnables(Runnable... runnables)
Runs the argument runnables concurrently.

See ParallelRunner for more information.

runnablesThe runnables.
public static void setInheritableDefaultThreadFactor(int threadfactor)
This method doesn't do anything anymore. Not recommended to use, keep track of your desired thread factor, and pass it to the appropriate configuration parameters instead of using an inheritable thread local configuration.
Sets the default thread factor that newly created concurrent runners should use.

This method used to set an inheritable thread local variable, whose value would be returned by getDefaultThreadFactor(). This feature is deprecated and that method returns a fixed value based on the available processor count.

threadfactorThe thread factor to use.
public static Thread startDaemonThread(Runnable runnable) throws NullPointerException
Starts a new daemon thread with the given name that executes the argument runnable.
runnableThe runnable.
The started thread.
NullPointerExceptionIf the runnable is null.
public static Thread startDaemonThread(String name, Runnable runnable) throws NullPointerException
Starts a new daemon thread with the given name that executes the argument runnable.
nameThe name of the new thread.
runnableThe runnable.
The started thread.
NullPointerExceptionIf the runnable is null.
public static Thread startDaemonThread(ThreadGroup group, Runnable runnable) throws NullPointerException
Starts a new daemon thread on the given thread group that executes the argument runnable.
groupThe thread group to start the thread on.
runnableThe runnable.
The started thread.
NullPointerExceptionIf the runnable is null.
public static Thread startDaemonThread(ThreadGroup group, String name, Runnable runnable) throws NullPointerException
Starts a new daemon thread on the given thread group with the specified name that executes the argument runnable.
groupThe thread group to start the thread on.
nameThe name of the new thread.
runnableThe runnable.
The started thread.
NullPointerExceptionIf the runnable is null.
public static Thread startThread(Runnable runnable) throws NullPointerException
Starts a new thread that executes the argument runnable.
runnableThe runnable.
The started thread.
NullPointerExceptionIf the runnable is null.
public static Thread startThread(String name, Runnable runnable) throws NullPointerException
Starts a new thread with the given name that executes the argument runnable.
nameThe name of the new thread.
runnableThe runnable.
The started thread.
NullPointerExceptionIf the runnable is null.
public static Thread startThread(ThreadGroup group, Runnable runnable) throws NullPointerException
Starts a new thread on the given thread group that executes the argument runnable.
groupThe thread group to start the thread on.
runnableThe runnable.
The started thread.
NullPointerExceptionIf the runnable is null.
public static Thread startThread(ThreadGroup group, String name, Runnable runnable) throws NullPointerException
Starts a new thread on the given thread group with the specified name that executes the argument runnable.
groupThe thread group to start the thread on.
nameThe name of the new thread.
runnableThe runnable.
The started thread.
NullPointerExceptionIf the runnable is null.