public class ThreadUtils extends Object
Modifier and Type | Method and Description |
---|---|
static ThreadFactory |
createThreadFactory(String nameFormat)
Creates new thread factory with specified thread name format.
|
static ThreadFactory |
createThreadFactory(String nameFormat,
Thread.UncaughtExceptionHandler exceptionHandler)
Creates new thread factory with specified thread name format.
|
static Thread |
startThread(Runnable runnable,
String threadName)
Start thread with specified name and runnable, and make it daemon
|
static Thread |
startThread(Runnable runnable,
String threadName,
Thread.UncaughtExceptionHandler handler)
Start thread with specified name, runnable and exception handler, and make
it daemon
|
static <T> Future<T> |
submitToExecutor(ExecutorService executorService,
Callable<T> callable,
Thread.UncaughtExceptionHandler uncaughtExceptionHandler)
Submit a callable to executor service, ensuring any exceptions are
caught with provided exception handler.
|
static boolean |
trySleep(long millis)
Sleep for specified milliseconds, logging and ignoring interrupted
exceptions
|
public static ThreadFactory createThreadFactory(String nameFormat, Thread.UncaughtExceptionHandler exceptionHandler)
nameFormat
- defines naming format for threads created by
thread factoryexceptionHandler
- handles uncaught exceptions in all threads
produced created thread factorypublic static ThreadFactory createThreadFactory(String nameFormat)
nameFormat
- defines naming format for threads created by
thread factorypublic static <T> Future<T> submitToExecutor(ExecutorService executorService, Callable<T> callable, Thread.UncaughtExceptionHandler uncaughtExceptionHandler)
T
- Type of callable resultexecutorService
- Executor service to submit callable tocallable
- Callable to submituncaughtExceptionHandler
- Handler for uncaught exceptions in callablepublic static Thread startThread(Runnable runnable, String threadName)
threadName
- Name of the threadrunnable
- Runnable to executepublic static Thread startThread(Runnable runnable, String threadName, Thread.UncaughtExceptionHandler handler)
runnable
- Runnable to executethreadName
- Name of the threadhandler
- Exception handlerpublic static boolean trySleep(long millis)
millis
- How long to sleep forCopyright © 2011-2020 The Apache Software Foundation. All Rights Reserved.