public class ThreadPool
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static interface |
ThreadPool.ContainerOfThreads |
static class |
ThreadPool.RecordingScheduledExecutor |
static class |
ThreadPool.RecordingThreadPool |
static class |
ThreadPool.Singleton<T>
Singletons are helpers that gatekeep execution of work on a service to a single runnable
at a time.
|
static class |
ThreadPool.SingletonResult<T>
SingletonResults are returned from
Singletons as a token
that can be used to await the completion of submitted work. |
static interface |
ThreadPool.ThreadBorrowable
Indicates a thread pool that is at times capable of using guest worker threads
|
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
TAG |
Constructor and Description |
---|
ThreadPool() |
Modifier and Type | Method and Description |
---|---|
static boolean |
awaitFuture(java.util.concurrent.Future future,
long timeout,
java.util.concurrent.TimeUnit unit) |
static boolean |
awaitTermination(java.util.concurrent.ExecutorService executorService,
long timeout,
java.util.concurrent.TimeUnit unit,
java.lang.String serviceName)
Waits for the indicated timeout for the indicated executor service to shutdown.
|
static void |
awaitTerminationOrExitApplication(java.util.concurrent.ExecutorService executorService,
long timeout,
java.util.concurrent.TimeUnit unit,
java.lang.String serviceName,
java.lang.String message)
Awaits the termination of the indicated service for the indicated timeout.
|
static void |
cancelFutureOrExitApplication(java.util.concurrent.Future future,
long timeout,
java.util.concurrent.TimeUnit unit,
java.lang.String serviceName,
java.lang.String message) |
static void |
exitApplication(java.lang.String serviceName,
java.lang.String message) |
static java.util.concurrent.ExecutorService |
getDefault() |
static java.util.concurrent.ScheduledExecutorService |
getDefaultScheduler() |
static java.util.concurrent.ExecutorService |
getDefaultSerial() |
static int |
getTID(long threadId)
Returns the OS-level thread id for the indicated Android-level thread id
|
static int |
getTID(java.lang.Thread thread)
Returns the OS-level thread id for the given thread
|
static void |
logThreadLifeCycle(java.lang.String name,
java.lang.Runnable runnable)
Robustly log thread startup and shutdown
|
static java.util.concurrent.ExecutorService |
newCachedThreadPool(java.lang.String nameRoot)
Creates a thread pool that creates new threads as needed, but will reuse previously constructed
threads when they are available.
|
static java.util.concurrent.ExecutorService |
newFixedThreadPool(int numberOfThreads,
java.lang.String nameRoot)
Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded
queue.
|
static ThreadPool.RecordingScheduledExecutor |
newScheduledExecutor(int maxWorkerThreadCount,
java.lang.String nameRoot)
Creates an executor that can schedule commands to run after a given delay, or
to execute periodically.
|
static java.util.concurrent.ExecutorService |
newSingleThreadExecutor(java.lang.String nameRoot)
Creates an Executor that uses a single worker thread operating off an unbounded queue.
|
protected static java.lang.Throwable |
retrieveUserException(java.lang.Runnable r,
java.lang.Throwable t) |
public static final java.lang.String TAG
public static java.util.concurrent.ExecutorService getDefault()
public static java.util.concurrent.ExecutorService getDefaultSerial()
public static java.util.concurrent.ScheduledExecutorService getDefaultScheduler()
public static java.util.concurrent.ExecutorService newSingleThreadExecutor(java.lang.String nameRoot)
Executors.newSingleThreadExecutor()
public static java.util.concurrent.ExecutorService newFixedThreadPool(int numberOfThreads, java.lang.String nameRoot)
numberOfThreads
threads will be active processing tasks.Executors.newFixedThreadPool(int)
public static java.util.concurrent.ExecutorService newCachedThreadPool(java.lang.String nameRoot)
Executors.newCachedThreadPool()
public static ThreadPool.RecordingScheduledExecutor newScheduledExecutor(int maxWorkerThreadCount, java.lang.String nameRoot)
Executors.newSingleThreadScheduledExecutor()
public static int getTID(java.lang.Thread thread)
public static int getTID(long threadId)
public static boolean awaitTermination(java.util.concurrent.ExecutorService executorService, long timeout, java.util.concurrent.TimeUnit unit, java.lang.String serviceName) throws java.lang.InterruptedException
executorService
- the service to shutdowntimeout
- the duration to wait for the shutdownunit
- the units in which the timeout parameter is providedserviceName
- the name of the service as it is to be recorded in the logjava.lang.InterruptedException
public static void awaitTerminationOrExitApplication(java.util.concurrent.ExecutorService executorService, long timeout, java.util.concurrent.TimeUnit unit, java.lang.String serviceName, java.lang.String message)
executorService
- the executor service whose termination we are to awaittimeout
- the timeout we are to permitunit
- the units in which timeout is expressedserviceName
- a human-understandable name for the executor servicemessage
- the message to print if we terminate the apppublic static boolean awaitFuture(java.util.concurrent.Future future, long timeout, java.util.concurrent.TimeUnit unit)
public static void cancelFutureOrExitApplication(java.util.concurrent.Future future, long timeout, java.util.concurrent.TimeUnit unit, java.lang.String serviceName, java.lang.String message)
public static void exitApplication(java.lang.String serviceName, java.lang.String message)
public static void logThreadLifeCycle(java.lang.String name, java.lang.Runnable runnable)
protected static java.lang.Throwable retrieveUserException(java.lang.Runnable r, java.lang.Throwable t)
ThreadPoolExecutor#afterExecute(Runnable, Throwable), after which this logic is modelled