private static final Map>TYPE_PRIORITY_POOLS =new HashMap<>();
private static final MapTASK_TASKINFO_MAP =new ConcurrentHashMap<>();
private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
private static final TimerTIMER =new Timer();
private static final byte TYPE_SINGLE = -1;
private static final byte TYPE_CACHED = -2;
private static final byte TYPE_IO = -4;
private static final byte TYPE_CPU = -8;
private static ExecutorsDeliver;
/**
* Return whether the thread is the main thread.
*
* @return {@code true}: yes
{@code false}: no
*/
public static boolean isMainThread() {
return Looper.myLooper() == Looper.getMainLooper();
}
/**
* Return a thread pool that reuses a fixed number of threads
* operating off a shared unbounded queue, using the provided
* ThreadFactory to create new threads when needed.
*
* @param size The size of thread in the pool.
* @return a fixed thread pool
*/
public static ExecutorServicegetFixedPool(@IntRange(from =1)final int size) {
return getPoolByTypeAndPriority(size);
}
/**
* Return a thread pool that reuses a fixed number of threads
* operating off a shared unbounded queue, using the provided
* ThreadFactory to create new threads when needed.
*
* @param size The size of thread in the pool.
* @param priority The priority of thread in the poll.
* @return a fixed thread pool
*/
public static ExecutorServicegetFixedPool(@IntRange(from =1)final int size,
@IntRange(from =1, to =10)final int priority) {
return getPoolByTypeAndPriority(size, priority);
}
/**
* Return a thread pool that uses a single worker thread operating
* off an unbounded queue, and uses the provided ThreadFactory to
* create a new thread when needed.
*
* @return a single thread pool
*/
public static ExecutorServicegetSinglePool() {
return getPoolByTypeAndPriority(TYPE_SINGLE);
}
/**
* Return a thread pool that uses a single worker thread operating
* off an unbounded queue, and uses the provided ThreadFactory to
* create a new thread when needed.
*
* @param priority The priority of thread in the poll.
* @return a single thread pool
*/
public static ExecutorServicegetSinglePool(@IntRange(from =1, to =10)final int priority) {
return getPoolByTypeAndPriority(TYPE_SINGLE, priority);
}
/**
* Return a thread pool that creates new threads as needed, but
* will reuse previously constructed threads when they are
* available.
*
* @return a cached thread pool
*/
public static ExecutorServicegetCachedPool() {
return getPoolByTypeAndPriority(TYPE_CACHED);
}
/**
* Return a thread pool that creates new threads as needed, but
* will reuse previously constructed threads when they are
* available.
*
* @param priority The priority of thread in the poll.
* @return a cached thread pool
*/
public static ExecutorServicegetCachedPool(@IntRange(from =1, to =10)final int priority) {
return getPoolByTypeAndPriority(TYPE_CACHED, priority);
}
/**
* Return a thread pool that creates (2 * CPU_COUNT + 1) threads
* operating off a queue which size is 128.
*
* @return a IO thread pool
*/
public static ExecutorServicegetIoPool() {
return getPoolByTypeAndPriority(TYPE_IO);
}
/**
* Return a thread pool that creates (2 * CPU_COUNT + 1) threads
* operating off a queue which size is 128.
*
* @param priority The priority of thread in the poll.
* @return a IO thread pool
*/
public static ExecutorServicegetIoPool(@IntRange(from =1, to =10)final int priority) {
return getPoolByTypeAndPriority(TYPE_IO, priority);
}
/**
* Return a thread pool that creates (CPU_COUNT + 1) threads
* operating off a queue which size is 128 and the maximum
* number of threads equals (2 * CPU_COUNT + 1).
*
* @return a cpu thread pool for
*/
public static ExecutorServicegetCpuPool() {
return getPoolByTypeAndPriority(TYPE_CPU);
}
/**
* Return a thread pool that creates (CPU_COUNT + 1) threads
* operating off a queue which size is 128 and the maximum
* number of threads equals (2 * CPU_COUNT + 1).
*
* @param priority The priority of thread in the poll.
* @return a cpu thread pool for
*/
public static ExecutorServicegetCpuPool(@IntRange(from =1, to =10)final int priority) {
return getPoolByTypeAndPriority(TYPE_CPU, priority);
}
/**
* Executes the given task in a fixed thread pool.
*
* @param size The size of thread in the fixed thread pool.
* @param task The task to execute.
* @param The type of the task's result.
*/
public static void executeByFixed(@IntRange(from =1)final int size, final Task task) {
execute(getPoolByTypeAndPriority(size), task);
}
/**
* Executes the given task in a fixed thread pool.
*
* @param size The size of thread in the fixed thread pool.
* @param task The task to execute.
* @param priority The priority of thread in the poll.
* @param The type of the task's result.
*/
public static void executeByFixed(@IntRange(from =1)final int size,
final Task task,
@IntRange(from =1, to =10)final int priority) {
execute(getPoolByTypeAndPriority(size, priority), task);
}
/**
* Executes the given task in a fixed thread pool after the given delay.
*
* @param size The size of thread in the fixed thread pool.
* @param task The task to execute.
* @param delay The time from now to delay execution.
* @param unit The time unit of the delay parameter.
* @param The type of the task's result.
*/
public static void executeByFixedWithDelay(@IntRange(from =1)final int size,
final Task task,
final long delay,
final TimeUnit unit) {
executeWithDelay(getPoolByTypeAndPriority(size), task, delay, unit);
}
/**
* Executes the given task in a fixed thread pool after the given delay.
*
* @param size The size of thread in the fixed thread pool.
* @param task The task to execute.
* @param delay The time from now to delay execution.
* @param unit The time unit of the delay parameter.
* @param priority The priority of thread in the poll.
* @param The type of the task's result.
*/
public static void executeByFixedWithDelay(@IntRange(from =1)final int size,
final Task task,
final long delay,
final TimeUnit unit,
@IntRange(from =1, to =10)final int priority) {
executeWithDelay(getPoolByTypeAndPriority(size, priority), task, delay, unit);
}
/**
* Executes the given task in a fixed thread pool at fix rate.
*
* @param size The size of thread in the fixed thread pool.
* @param task The task to execute.
* @param period The period between successive executions.
* @param unit The time unit of the period parameter.
* @param The type of the task's result.
*/
public static void executeByFixedAtFixRate(@IntRange(from =1)final int size,
final Task task,
final long period,
final TimeUnit unit) {
executeAtFixedRate(getPoolByTypeAndPriority(size), task, 0, period, unit);
}
/**
* Executes the given task in a fixed thread pool at fix rate.
*
* @param size The size of thread in the fixed thread pool.
* @param task The task to execute.
* @param period The period between successive executions.
* @param unit The time unit of the period parameter.
* @param priority The priority of thread in the poll.
* @param The type of the task's result.
*/
public static void executeByFixedAtFixRate(@IntRange(from =1)final int size,
final Task task,
final long period,
final TimeUnit unit,
@IntRange(from =1, to =10)final int priority) {
executeAtFixedRate(getPoolByTypeAndPriority(size, priority), task, 0, period, unit);
}
/**
* Executes the given task in a fixed thread pool at fix rate.
*
* @param size The size of thread in the fixed thread pool.
* @param task The task to execute.
* @param initialDelay The time to delay first execution.
* @param period The period between successive executions.
* @param unit The time unit of the initialDelay and period parameters.
* @param The type of the task's result.
*/
public static void executeByFixedAtFixRate(@IntRange(from =1)final int size,
final Task task,
long initialDelay,
final long period,
final TimeUnit unit) {
executeAtFixedRate(getPoolByTypeAndPriority(size), task, initialDelay, period, unit);
}
/**
* Executes the given task in a fixed thread pool at fix rate.
*
* @param size The size of thread in the fixed thread pool.
* @param task The task to execute.
* @param initialDelay The time to delay first execution.
* @param period The period between successive executions.
* @param unit The time unit of the initialDelay and period parameters.
* @param priority The priority of thread in the poll.
* @param The type of the task's result.
*/
public static void executeByFixedAtFixRate(@IntRange(from =1)final int size,
final Task task,
long initialDelay,
final long period,
final TimeUnit unit,
@IntRange(from =1, to =10)final int priority) {
executeAtFixedRate(getPoolByTypeAndPriority(size, priority), task, initialDelay, period, unit);
}
/**
* Executes the given task in a single thread pool.
*
* @param task The task to execute.
* @param The type of the task's result.
*/
public static void executeBySingle(final Task task) {
execute(getPoolByTypeAndPriority(TYPE_SINGLE), task);
}
/**
* Executes the given task in a single thread pool.
*
* @param task The task to execute.
* @param priority The priority of thread in the poll.
* @param The type of the task's result.
*/
public static void executeBySingle(final Task task,
@IntRange(from =1, to =10)final int priority) {
execute(getPoolByTypeAndPriority(TYPE_SINGLE, priority), task);
}
/**
* Executes the given task in a single thread pool after the given delay.
*
* @param task The task to execute.
* @param delay The time from now to delay execution.
* @param unit The time unit of the delay parameter.
* @param The type of the task's result.
*/
public static void executeBySingleWithDelay(final Task task,
final long delay,
final TimeUnit unit) {
executeWithDelay(getPoolByTypeAndPriority(TYPE_SINGLE), task, delay, unit);
}
/**
* Executes the given task in a single thread pool after the given delay.
*
* @param task The task to execute.
* @param delay The time from now to delay execution.
* @param unit The time unit of the delay parameter.
* @param priority The priority of thread in the poll.
* @param The type of the task's result.
*/
public static void executeBySingleWithDelay(final Task task,
final long delay,
final TimeUnit unit,
@IntRange(from =1, to =10)final int priority) {
executeWithDelay(getPoolByTypeAndPriority(TYPE_SINGLE, priority), task, delay, unit);
}
/**
* Executes the given task in a single thread pool at fix rate.
*
* @param task The task to execute.
* @param period The period between successive executions.
* @param unit The time unit of the period parameter.
* @param The type of the task's result.
*/
public static void executeBySingleAtFixRate(final Task task,
final long period,
final TimeUnit unit) {
executeAtFixedRate(getPoolByTypeAndPriority(TYPE_SINGLE), task, 0, period, unit);
}
/**
* Executes the given task in a single thread pool at fix rate.
*
* @param task The task to execute.
* @param period The period between successive executions.
* @param unit The time unit of the period parameter.
* @param priority The priority of thread in the poll.
* @param The type of the task's result.
*/
public static void executeBySingleAtFixRate(final Task task,
final long period,
final TimeUnit unit,
@IntRange(from =1, to =10)final int priority) {
executeAtFixedRate(getPoolByTypeAndPriority(TYPE_SINGLE, priority), task, 0, period, unit);
}
/**
* Executes the given task in a single thread pool at fix rate.
*
* @param task The task to execute.
* @param initialDelay The time to delay first execution.
* @param period The period between successive executions.
* @param unit The time unit of the initialDelay and period parameters.
* @param The type of the task's result.
*/
public static void executeBySingleAtFixRate(final Task task,
long initialDelay,
final long period,
final TimeUnit unit) {
executeAtFixedRate(getPoolByTypeAndPriority(TYPE_SINGLE), task, initialDelay, period, unit);
}
/**
* Executes the given task in a single thread pool at fix rate.
*
* @param task The task to execute.
* @param initialDelay The time to delay first execution.
* @param period The period between successive executions.
* @param unit The time unit of the initialDelay and period parameters.
* @param priority The priority of thread in the poll.
* @param The type of the task's result.
*/
public static void executeBySingleAtFixRate(final Task task,
long initialDelay,
final long period,
final TimeUnit unit,
@IntRange(from =1, to =10)final int priority) {
executeAtFixedRate(
getPoolByTypeAndPriority(TYPE_SINGLE, priority), task, initialDelay, period, unit
);
}
/**
* Executes the given task in a cached thread pool.
*
* @param task The task to execute.
* @param The type of the task's result.
*/
public static void executeByCached(final Task task) {
execute(getPoolByTypeAndPriority(TYPE_CACHED), task);
}
/**
* Executes the given task in a cached thread pool.
*
* @param task The task to execute.
* @param priority The priority of thread in the poll.
* @param The type of the task's result.
*/
public static void executeByCached(final Task task,
@IntRange(from =1, to =10)final int priority) {
execute(getPoolByTypeAndPriority(TYPE_CACHED, priority), task);
}
/**
* Executes the given task in a cached thread pool after the given delay.
*
* @param task The task to execute.
* @param delay The time from now to delay execution.
* @param unit The time unit of the delay parameter.
* @param The type of the task's result.
*/
public static void executeByCachedWithDelay(final Task task,
final long delay,
final TimeUnit unit) {
executeWithDelay(getPoolByTypeAndPriority(TYPE_CACHED), task, delay, unit);
}
/**
* Executes the given task in a cached thread pool after the given delay.
*
* @param task The task to execute.
* @param delay The time from now to delay execution.
* @param unit The time unit of the delay parameter.
* @param priority The priority of thread in the poll.
* @param The type of the task's result.
*/
public static void executeByCachedWithDelay(final Task task,
final long delay,
final TimeUnit unit,
@IntRange(from =1, to =10)final int priority) {
executeWithDelay(getPoolByTypeAndPriority(TYPE_CACHED, priority), task, delay, unit);
}
/**
* Executes the given task in a cached thread pool at fix rate.
*
* @param task The task to execute.
* @param period The period between successive executions.
* @param unit The time unit of the period parameter.
* @param The type of the task's result.
*/
public static void executeByCachedAtFixRate(final Task task,
final long period,
final TimeUnit unit) {
executeAtFixedRate(getPoolByTypeAndPriority(TYPE_CACHED), task, 0, period, unit);
}
/**
* Executes the given task in a cached thread pool at fix rate.
*
* @param task The task to execute.
* @param period The period between successive executions.
* @param unit The time unit of the period parameter.
* @param priority The priority of thread in the poll.
* @param The type of the task's result.
*/
public static void executeByCachedAtFixRate(final Task task,
final long period,
final TimeUnit unit,
@IntRange(from =1, to =10)final int priority) {
executeAtFixedRate(getPoolByTypeAndPriority(TYPE_CACHED, priority), task, 0, period, unit);
}
/**
* Executes the given task in a cached thread pool at fix rate.
*
* @param task The task to execute.
* @param initialDelay The time to delay first execution.
* @param period The period between successive executions.
* @param unit The time unit of the initialDelay and period parameters.
* @param The type of the task's result.
*/
public static void executeByCachedAtFixRate(final Task task,
long initialDelay,
final long period,
final TimeUnit unit) {
executeAtFixedRate(getPoolByTypeAndPriority(TYPE_CACHED), task, initialDelay, period, unit);
}
/**
* Executes the given task in a cached thread pool at fix rate.
*
* @param task The task to execute.
* @param initialDelay The time to delay first execution.
* @param period The period between successive executions.
* @param unit The time unit of the initialDelay and period parameters.
* @param priority The priority of thread in the poll.
* @param The type of the task's result.
*/
public static void executeByCachedAtFixRate(final Task task,
long initialDelay,
final long period,
final TimeUnit unit,
@IntRange(from =1, to =10)final int priority) {
executeAtFixedRate(
getPoolByTypeAndPriority(TYPE_CACHED, priority), task, initialDelay, period, unit
);
}
/**
* Executes the given task in an IO thread pool.
*
* @param task The task to execute.
* @param The type of the task's result.
*/
public static void executeByIo(final Task task) {
execute(getPoolByTypeAndPriority(TYPE_IO), task);
}
/**
* Executes the given task in an IO thread pool.
*
* @param task The task to execute.
* @param priority The priority of thread in the poll.
* @param The type of the task's result.
*/
public static void executeByIo(final Task task,
@IntRange(from =1, to =10)final int priority) {
execute(getPoolByTypeAndPriority(TYPE_IO, priority), task);
}
/**
* Executes the given task in an IO thread pool after the given delay.
*
* @param task The task to execute.
* @param delay The time from now to delay execution.
* @param unit The time unit of the delay parameter.
* @param The type of the task's result.
*/
public static void executeByIoWithDelay(final Task task,
final long delay,
final TimeUnit unit) {
executeWithDelay(getPoolByTypeAndPriority(TYPE_IO), task, delay, unit);
}
/**
* Executes the given task in an IO thread pool after the given delay.
*
* @param task The task to execute.
* @param delay The time from now to delay execution.
* @param unit The time unit of the delay parameter.
* @param priority The priority of thread in the poll.
* @param The type of the task's result.
*/
public static void executeByIoWithDelay(final Task task,
final long delay,
final TimeUnit unit,
@IntRange(from =1, to =10)final int priority) {
executeWithDelay(getPoolByTypeAndPriority(TYPE_IO, priority), task, delay, unit);
}
/**
* Executes the given task in an IO thread pool at fix rate.
*
* @param task The task to execute.
* @param period The period between successive executions.
* @param unit The time unit of the period parameter.
* @param The type of the task's result.
*/
public static void executeByIoAtFixRate(final Task task,
final long period,
final TimeUnit unit) {
executeAtFixedRate(getPoolByTypeAndPriority(TYPE_IO), task, 0, period, unit);
}
/**
* Executes the given task in an IO thread pool at fix rate.
*
* @param task The task to execute.
* @param period The period between successive executions.
* @param unit The time unit of the period parameter.
* @param priority The priority of thread in the poll.
* @param The type of the task's result.
*/
public static void executeByIoAtFixRate(final Task task,
final long period,
final TimeUnit unit,
@IntRange(from =1, to =10)final int priority) {
executeAtFixedRate(getPoolByTypeAndPriority(TYPE_IO, priority), task, 0, period, unit);
}
/**
* Executes the given task in an IO thread pool at fix rate.
*
* @param task The task to execute.
* @param initialDelay The time to delay first execution.
* @param period The period between successive executions.
* @param unit The time unit of the initialDelay and period parameters.
* @param The type of the task's result.
*/
public static void executeByIoAtFixRate(final Task task,
long initialDelay,
final long period,
final TimeUnit unit) {
executeAtFixedRate(getPoolByTypeAndPriority(TYPE_IO), task, initialDelay, period, unit);
}
/**
* Executes the given task in an IO thread pool at fix rate.
*
* @param task The task to execute.
* @param initialDelay The time to delay first execution.
* @param period The period between successive executions.
* @param unit The time unit of the initialDelay and period parameters.
* @param priority The priority of thread in the poll.
* @param The type of the task's result.
*/
public static void executeByIoAtFixRate(final Task task,
long initialDelay,
final long period,
final TimeUnit unit,
@IntRange(from =1, to =10)final int priority) {
executeAtFixedRate(
getPoolByTypeAndPriority(TYPE_IO, priority), task, initialDelay, period, unit
);
}
/**
* Executes the given task in a cpu thread pool.
*
* @param task The task to execute.
* @param The type of the task's result.
*/
public static void executeByCpu(final Task task) {
execute(getPoolByTypeAndPriority(TYPE_CPU), task);
}
/**
* Executes the given task in a cpu thread pool.
*
* @param task The task to execute.
* @param priority The priority of thread in the poll.
* @param The type of the task's result.
*/
public static void executeByCpu(final Task task,
@IntRange(from =1, to =10)final int priority) {
execute(getPoolByTypeAndPriority(TYPE_CPU, priority), task);
}
/**
* Executes the given task in a cpu thread pool after the given delay.
*
* @param task The task to execute.
* @param delay The time from now to delay execution.
* @param unit The time unit of the delay parameter.
* @param The type of the task's result.
*/
public static void executeByCpuWithDelay(final Task task,
final long delay,
final TimeUnit unit) {
executeWithDelay(getPoolByTypeAndPriority(TYPE_CPU), task, delay, unit);
}
/**
* Executes the given task in a cpu thread pool after the given delay.
*
* @param task The task to execute.
* @param delay The time from now to delay execution.
* @param unit The time unit of the delay parameter.
* @param priority The priority of thread in the poll.
* @param The type of the task's result.
*/
public static void executeByCpuWithDelay(final Task task,
final long delay,
final TimeUnit unit,
@IntRange(from =1, to =10)final int priority) {
executeWithDelay(getPoolByTypeAndPriority(TYPE_CPU, priority), task, delay, unit);
}
/**
* Executes the given task in a cpu thread pool at fix rate.
*
* @param task The task to execute.
* @param period The period between successive executions.
* @param unit The time unit of the period parameter.
* @param The type of the task's result.
*/
public static void executeByCpuAtFixRate(final Task task,
final long period,
final TimeUnit unit) {
executeAtFixedRate(getPoolByTypeAndPriority(TYPE_CPU), task, 0, period, unit);
}
/**
* Executes the given task in a cpu thread pool at fix rate.
*
* @param task The task to execute.
* @param period The period between successive executions.
* @param unit The time unit of the period parameter.
* @param priority The priority of thread in the poll.
* @param The type of the task's result.
*/
public static void executeByCpuAtFixRate(final Task task,
final long period,
final TimeUnit unit,
@IntRange(from =1, to =10)final int priority) {
executeAtFixedRate(getPoolByTypeAndPriority(TYPE_CPU, priority), task, 0, period, unit);
}
/**
* Executes the given task in a cpu thread pool at fix rate.
*
* @param task The task to execute.
* @param initialDelay The time to delay first execution.
* @param period The period between successive executions.
* @param unit The time unit of the initialDelay and period parameters.
* @param The type of the task's result.
*/
public static void executeByCpuAtFixRate(final Task task,
long initialDelay,
final long period,
final TimeUnit unit) {
executeAtFixedRate(getPoolByTypeAndPriority(TYPE_CPU), task, initialDelay, period, unit);
}
/**
* Executes the given task in a cpu thread pool at fix rate.
*
* @param task The task to execute.
* @param initialDelay The time to delay first execution.
* @param period The period between successive executions.
* @param unit The time unit of the initialDelay and period parameters.
* @param priority The priority of thread in the poll.
* @param The type of the task's result.
*/
public static void executeByCpuAtFixRate(final Task task,
long initialDelay,
final long period,
final TimeUnit unit,
@IntRange(from =1, to =10)final int priority) {
executeAtFixedRate(
getPoolByTypeAndPriority(TYPE_CPU, priority), task, initialDelay, period, unit
);
}
/**
* Executes the given task in a custom thread pool.
*
* @param pool The custom thread pool.
* @param task The task to execute.
* @param The type of the task's result.
*/
public static void executeByCustom(final ExecutorService pool, final Task task) {
execute(pool, task);
}
/**
* Executes the given task in a custom thread pool after the given delay.
*
* @param pool The custom thread pool.
* @param task The task to execute.
* @param delay The time from now to delay execution.
* @param unit The time unit of the delay parameter.
* @param The type of the task's result.
*/
public static void executeByCustomWithDelay(final ExecutorService pool,
final Task task,
final long delay,
final TimeUnit unit) {
executeWithDelay(pool, task, delay, unit);
}
/**
* Executes the given task in a custom thread pool at fix rate.
*
* @param pool The custom thread pool.
* @param task The task to execute.
* @param period The period between successive executions.
* @param unit The time unit of the period parameter.
* @param The type of the task's result.
*/
public static void executeByCustomAtFixRate(final ExecutorService pool,
final Task task,
final long period,
final TimeUnit unit) {
executeAtFixedRate(pool, task, 0, period, unit);
}
/**
* Executes the given task in a custom thread pool at fix rate.
*
* @param pool The custom thread pool.
* @param task The task to execute.
* @param initialDelay The time to delay first execution.
* @param period The period between successive executions.
* @param unit The time unit of the initialDelay and period parameters.
* @param The type of the task's result.
*/
public static void executeByCustomAtFixRate(final ExecutorService pool,
final Task task,
long initialDelay,
final long period,
final TimeUnit unit) {
executeAtFixedRate(pool, task, initialDelay, period, unit);
}
/**
* Cancel the given task.
*
* @param task The task to cancel.
*/
public static void cancel(final Task task) {
if (task ==null)return;
task.cancel();
}
/**
* Cancel the given tasks.
*
* @param tasks The tasks to cancel.
*/
public static void cancel(final Task... tasks) {
if (tasks ==null || tasks.length ==0)return;
for (Task task : tasks) {
if (task ==null)continue;
task.cancel();
}
}
/**
* Cancel the given tasks.
*
* @param tasks The tasks to cancel.
*/
public static void cancel(final List tasks) {
if (tasks ==null || tasks.size() ==0)return;
for (Task task : tasks) {
if (task ==null)continue;
task.cancel();
}
}
/**
* Cancel the tasks in pool.
*
* @param executorService The pool.
*/
public static void cancel(ExecutorService executorService) {
if (executorServiceinstanceof ThreadPoolExecutor4Util) {
for (Map.Entry taskTaskInfoEntry :TASK_TASKINFO_MAP.entrySet()) {
if (taskTaskInfoEntry.getValue().mService == executorService) {
cancel(taskTaskInfoEntry.getKey());
}
}
}else {
Log.e("LogUtils", "The executorService is not ThreadUtils's pool.");
}
}
/**
* Set the deliver.
*
* @param deliver The deliver.
*/
public static void setDeliver(final Executor deliver) {
sDeliver = deliver;
}
private static void execute(final ExecutorService pool, final Task task) {
execute(pool, task, 0, 0, null);
}
private static void executeWithDelay(final ExecutorService pool,
final Task task,
final long delay,
final TimeUnit unit) {
execute(pool, task, delay, 0, unit);
}
private static void executeAtFixedRate(final ExecutorService pool,
final Task task,
long delay,
final long period,
final TimeUnit unit) {
execute(pool, task, delay, period, unit);
}
private static void execute(final ExecutorService pool, final Task task,
long delay, final long period, final TimeUnit unit) {
TaskInfo taskInfo;
synchronized (TASK_TASKINFO_MAP) {
if (TASK_TASKINFO_MAP.get(task) !=null) {
Log.e("ThreadUtils", "Task can only be executed once.");
return;
}
taskInfo =new TaskInfo(pool);
TASK_TASKINFO_MAP.put(task, taskInfo);
}
if (period ==0) {
if (delay ==0) {
pool.execute(task);
}else {
TimerTask timerTask =new TimerTask() {
@Override
public void run() {
pool.execute(task);
}
};
taskInfo.mTimerTask = timerTask;
TIMER.schedule(timerTask, unit.toMillis(delay));
}
}else {
task.setSchedule(true);
TimerTask timerTask =new TimerTask() {
@Override
public void run() {
pool.execute(task);
}
};
taskInfo.mTimerTask = timerTask;
TIMER.scheduleAtFixedRate(timerTask, unit.toMillis(delay), unit.toMillis(period));
}
}
private static ExecutorServicegetPoolByTypeAndPriority(final int type) {
return getPoolByTypeAndPriority(type, Thread.NORM_PRIORITY);
}
private static ExecutorServicegetPoolByTypeAndPriority(final int type, final int priority) {
synchronized (TYPE_PRIORITY_POOLS) {
ExecutorService pool;
Map priorityPools =TYPE_PRIORITY_POOLS.get(type);
if (priorityPools ==null) {
priorityPools =new ConcurrentHashMap<>();
pool = ThreadPoolExecutor4Util.createPool(type, priority);
priorityPools.put(priority, pool);
TYPE_PRIORITY_POOLS.put(type, priorityPools);
}else {
pool = priorityPools.get(priority);
if (pool ==null) {
pool = ThreadPoolExecutor4Util.createPool(type, priority);
priorityPools.put(priority, pool);
}
}
return pool;
}
}
static final class ThreadPoolExecutor4Utilextends ThreadPoolExecutor {
private static ExecutorServicecreatePool(final int type, final int priority) {
switch (type) {
case TYPE_SINGLE:
return new ThreadPoolExecutor4Util(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue4Util(),
new UtilsThreadFactory("single", priority)
);
case TYPE_CACHED:
return new ThreadPoolExecutor4Util(0, 128,
60L, TimeUnit.SECONDS,
new LinkedBlockingQueue4Util(true),
new UtilsThreadFactory("cached", priority)
);
case TYPE_IO:
return new ThreadPoolExecutor4Util(2 *CPU_COUNT +1, 2 *CPU_COUNT +1,
30, TimeUnit.SECONDS,
new LinkedBlockingQueue4Util(),
new UtilsThreadFactory("io", priority)
);
case TYPE_CPU:
return new ThreadPoolExecutor4Util(CPU_COUNT +1, 2 *CPU_COUNT +1,
30, TimeUnit.SECONDS,
new LinkedBlockingQueue4Util(true),
new UtilsThreadFactory("cpu", priority)
);
default:
return new ThreadPoolExecutor4Util(type, type,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue4Util(),
new UtilsThreadFactory("fixed(" + type +")", priority)
);
}
}
private final AtomicIntegermSubmittedCount =new AtomicInteger();
private LinkedBlockingQueue4UtilmWorkQueue;
ThreadPoolExecutor4Util(int corePoolSize, int maximumPoolSize,
long keepAliveTime, TimeUnit unit,
LinkedBlockingQueue4Util workQueue,
ThreadFactory threadFactory) {
super(corePoolSize, maximumPoolSize,
keepAliveTime, unit,
workQueue,
threadFactory
);
workQueue.mPool =this;
mWorkQueue = workQueue;
}
private int getSubmittedCount() {
return mSubmittedCount.get();
}
@Override
protected void afterExecute(Runnable r, Throwable t) {
mSubmittedCount.decrementAndGet();
super.afterExecute(r, t);
}
@Override
public void execute(@NonNull Runnable command) {
if (this.isShutdown())return;
mSubmittedCount.incrementAndGet();
try {
super.execute(command);
}catch (RejectedExecutionException ignore) {
Log.e("ThreadUtils", "This will not happen!");
mWorkQueue.offer(command);
}catch (Throwable t) {
mSubmittedCount.decrementAndGet();
}
}
}
private static final class LinkedBlockingQueue4Utilextends LinkedBlockingQueue {
private volatile ThreadPoolExecutor4UtilmPool;
private int mCapacity = Integer.MAX_VALUE;
LinkedBlockingQueue4Util() {
super();
}
LinkedBlockingQueue4Util(boolean isAddSubThreadFirstThenAddQueue) {
super();
if (isAddSubThreadFirstThenAddQueue) {
mCapacity =0;
}
}
LinkedBlockingQueue4Util(int capacity) {
super();
mCapacity = capacity;
}
@Override
public boolean offer(@NonNull Runnable runnable) {
if (mCapacity <= size() &&
mPool !=null &&mPool.getPoolSize()
// create a non-core thread
return false;
}
return super.offer(runnable);
}
}
private static final class UtilsThreadFactoryextends AtomicLong
implements ThreadFactory {
private static final AtomicIntegerPOOL_NUMBER =new AtomicInteger(1);
private static final long serialVersionUID = -9209200509960368598L;
private final StringnamePrefix;
private final int priority;
private final boolean isDaemon;
UtilsThreadFactory(String prefix, int priority) {
this(prefix, priority, false);
}
UtilsThreadFactory(String prefix, int priority, boolean isDaemon) {
namePrefix = prefix +"-pool-" +
POOL_NUMBER.getAndIncrement() +
"-thread-";
this.priority = priority;
this.isDaemon = isDaemon;
}
@Override
public ThreadnewThread(@NonNull Runnable r) {
Thread t =new Thread(r, namePrefix + getAndIncrement()) {
@Override
public void run() {
try {
super.run();
}catch (Throwable t) {
Log.e("ThreadUtils", "Request threw uncaught throwable", t);
}
}
};
t.setDaemon(isDaemon);
t.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
System.out.println(e);
}
});
t.setPriority(priority);
return t;
}
}
public abstract static class SimpleTaskextends Task {
@Override
public void onCancel() {
Log.e("ThreadUtils", "onCancel: " + Thread.currentThread());
}
@Override
public void onFail(Throwable t) {
Log.e("ThreadUtils", "onFail: ", t);
}
}
public abstract static class Taskimplements Runnable {
private static final int NEW =0;
private static final int RUNNING =1;
private static final int EXCEPTIONAL =2;
private static final int COMPLETING =3;
private static final int CANCELLED =4;
private static final int INTERRUPTED =5;
private static final int TIMEOUT =6;
private final AtomicIntegerstate =new AtomicInteger(NEW);
private volatile boolean isSchedule;
private volatile Threadrunner;
private TimermTimer;
private Executordeliver;
public abstract T doInBackground()throws Throwable;
public abstract void onSuccess(T result);
public abstract void onCancel();
public abstract void onFail(Throwable t);
@Override
public void run() {
if (isSchedule) {
if (runner ==null) {
if (!state.compareAndSet(NEW, RUNNING))return;
runner = Thread.currentThread();
}else {
if (state.get() !=RUNNING)return;
}
}else {
if (!state.compareAndSet(NEW, RUNNING))return;
runner = Thread.currentThread();
}
try {
final T result = doInBackground();
if (isSchedule) {
if (state.get() !=RUNNING)return;
getDeliver().execute(new Runnable() {
@Override
public void run() {
onSuccess(result);
}
});
}else {
if (!state.compareAndSet(RUNNING, COMPLETING))return;
getDeliver().execute(new Runnable() {
@Override
public void run() {
onSuccess(result);
onDone();
}
});
}
}catch (InterruptedException ignore) {
state.compareAndSet(CANCELLED, INTERRUPTED);
}catch (final Throwable throwable) {
if (!state.compareAndSet(RUNNING, EXCEPTIONAL))return;
getDeliver().execute(new Runnable() {
@Override
public void run() {
onFail(throwable);
onDone();
}
});
}
}
public void cancel() {
cancel(true);
}
public void cancel(boolean mayInterruptIfRunning) {
synchronized (state) {
if (state.get() >RUNNING)return;
state.set(CANCELLED);
}
if (mayInterruptIfRunning) {
if (runner !=null) {
runner.interrupt();
}
}
getDeliver().execute(new Runnable() {
@Override
public void run() {
onCancel();
onDone();
}
});
}
private void timeout() {
synchronized (state) {
if (state.get() >RUNNING)return;
state.set(TIMEOUT);
}
if (runner !=null) {
runner.interrupt();
}
onDone();
}
public boolean isCanceled() {
return state.get() >=CANCELLED;
}
public boolean isDone() {
return state.get() >RUNNING;
}
public TasksetDeliver(Executor deliver) {
this.deliver = deliver;
return this;
}
public void setTimeout(final long timeoutMillis, final OnTimeoutListener listener) {
mTimer =new Timer();
mTimer.schedule(new TimerTask() {
@Override
public void run() {
if (!isDone() &&listener !=null) {
timeout();
listener.onTimeout();
}
}
}, timeoutMillis);
}
private void setSchedule(boolean isSchedule) {
this.isSchedule = isSchedule;
}
private ExecutorgetDeliver() {
if (deliver ==null) {
return getGlobalDeliver();
}
return deliver;
}
@CallSuper
protected void onDone() {
TASK_TASKINFO_MAP.remove(this);
if (mTimer !=null) {
mTimer.cancel();
mTimer =null;
}
}
public interface OnTimeoutListener {
void onTimeout();
}
}
private static ExecutorgetGlobalDeliver() {
if (sDeliver ==null) {
sDeliver =new Executor() {
private final HandlermHandler =new Handler(Looper.getMainLooper());
@Override
public void execute(@NonNull Runnable command) {
mHandler.post(command);
}
};
}
return sDeliver;
}
private static class TaskInfo {
private TimerTaskmTimerTask;
private ExecutorServicemService;
private TaskInfo(ExecutorService service) {
mService = service;
}
}