public final class ThreadService extends Object
Modifier and Type | Class and Description |
---|---|
static class |
ThreadService.AbortRejectionPolicy
A
ThreadPoolRejectionHandler that simply throws an Exception. |
static class |
ThreadService.CallerRunsRejectionPolicy
A
ThreadPoolRejectionHandler which attempts to run the task in
the current thread. |
Modifier and Type | Field and Description |
---|---|
static String |
SCHEDULE_DAILY
Constant which represents a daily schedule.
|
static String |
SCHEDULE_EVERY_MINUTE
Constant which represents a cron entry which will execute every minute.
|
Modifier and Type | Method and Description |
---|---|
static void |
bootstrap(com.pushtechnology.diffusion.threads.ThreadPoolManager someThreadPoolManager,
com.pushtechnology.diffusion.scheduling.Scheduler someScheduler) |
static ThreadPool |
createThreadPool(String name,
int coreSize,
int maxSize,
int queueSize,
int priority)
Creates a new thread pool.
|
static ThreadPool |
getBackgroundThreadPool()
Returns the background thread pool.
|
static ThreadPool |
getThreadPool(String poolName)
Returns a named thread pool.
|
static List<ThreadPool> |
getThreadPools()
Returns the full list of currently defined thread pools.
|
static Thread |
newThread(Runnable runnable,
String name,
int priority,
boolean daemon)
This is a convenience method for creating a new thread.
|
static ScheduledFuture<?> |
schedule(RunnableTask runnable,
long initialDelay,
long period,
TimeUnit unit,
boolean isConstantRate)
Schedules a task for periodic execution.
|
static ScheduledFuture<?> |
schedule(RunnableTask runnable,
long delay,
TimeUnit unit)
Schedule a task for execution after a given period of time.
|
static Schedule |
schedule(ScheduledTask task)
Schedules a
ScheduledTask to run according to a frequency
schedule defined by the task itself. |
static Schedule |
schedule(String name,
String frequency,
RunnableTask task)
Schedules a task to be run according to a schedule specified by a 'cron'
like frequency pattern.
|
public static final String SCHEDULE_DAILY
public static final String SCHEDULE_EVERY_MINUTE
public static void bootstrap(com.pushtechnology.diffusion.threads.ThreadPoolManager someThreadPoolManager, com.pushtechnology.diffusion.scheduling.Scheduler someScheduler)
public static ThreadPool getThreadPool(String poolName)
poolName
- the pool namepublic static List<ThreadPool> getThreadPools()
public static ThreadPool createThreadPool(String name, int coreSize, int maxSize, int queueSize, int priority) throws APIException
name
- a name for the thread pool.coreSize
- the number of core threads that should be in the pool. As
threads are required then new threads will be created until this
number is reached even if there are already idle threads in the
pool.maxSize
- the maximum number of threads that may be created in the
pool. A value of 0 indicates that the pool should be effectively
unbounded (i.e. a value of Integer.MAX_VALUE
is assumed)
in which case the task queue would never be used.queueSize
- the size of the task queue. Tasks are queued if there
are no idle threads in the pool and the maximum pool size has been
reached.priority
- since 5.8 the priority cannot be changed and this
parameter is ignoredAPIException
- if unable to create the thread pool.public static Thread newThread(Runnable runnable, String name, int priority, boolean daemon)
The thread created will have an uncaught exception handler which will log exceptions.
runnable
- the runnablename
- the thread namepriority
- since 5.8 the priority cannot be changed and this
parameter is ignoreddaemon
- whether thread is to be a daemon (see
Thread.setDaemon(boolean)
)public static ThreadPool getBackgroundThreadPool() throws APIException
APIException
- if unable to get the background thread pool.public static Schedule schedule(ScheduledTask task) throws APIException
ScheduledTask
to run according to a frequency
schedule defined by the task itself.
Tasks are run using the Background thread pool.
task
- the task to schedule.Schedule
object that may be used to cancel the
schedule.APIException
- if unable to schedule the task.public static Schedule schedule(String name, String frequency, RunnableTask task) throws APIException
Tasks are run on the Background thread pool.
name
- a name that will identify the scheduled task.frequency
- the frequency at which to run the task, specified as a
'cron' pattern, this is broken into five parts, separated by
spaces:
1) Minutes sub-pattern.
During which minutes of the hour should the task been launched?
The values range is from 0 to 59.
2) Hours sub-pattern.
During which hours of the day should the task been launched? The
values range is from 0 to 23.
3) Days of month sub-pattern.
During which days of the month should the task been launched? The
values range is from 1 to 31. The special value "L" can be used to
recognise the last day of month.
4) Months sub-pattern.
During which months of the year should the task be launched? The
values range is from 1 (January) to 12 (December), otherwise this
sub-pattern allows the aliases "jan", "feb", "mar", "apr", "may",
"jun", "jul", "aug", "sep", "oct", "nov" and "dec".
5) Days of week sub-pattern.
During which days of the week should the task been launched? The
values range is from 0 (Sunday) to 6 (Saturday), otherwise this
sub-pattern allows the aliases "sun", "mon", "tue", "wed", "thu",
"fri" and "sat".
The '*' wildcard character is also admitted, indicating "every minute of the hour", "every hour of the day", "every day of the month", "every month of the year" and "every day of the week", according to the sub-pattern in which it is used.
task
- the task to runSchedule
object that may be used to cancel the
schedule.APIException
- if unable to schedule the taskpublic static ScheduledFuture<?> schedule(RunnableTask runnable, long delay, TimeUnit unit) throws APIException
The task is scheduled to run in the background thread pool.
runnable
- the task to run.delay
- time before execution.unit
- time unit of the delay parameter.APIException
- if unable to schedule the task.public static ScheduledFuture<?> schedule(RunnableTask runnable, long initialDelay, long period, TimeUnit unit, boolean isConstantRate) throws APIException
The task is first executed after the given initial delay, and subsequently at the given periodic interval.
Executions will commence after initialDelay then initialDelay+period, then initialDelay + 2 * period, and so on. If any execution of the task encounters an exception, subsequent executions are suppressed. Otherwise, the task will only terminate via cancellation or termination of the executor. As this is a background thread, this will be given a Thread.MIN_PRIORITY
Tasks are executed in the background thread pool.
runnable
- the task to execute.initialDelay
- the time to delay first execution.period
- the period between successive executions.unit
- the time unit of the initialDelay and period parametersisConstantRate
- If true then the period specified is the period
between successive initiations of the task. If false then the
period is the delay between successive runs of the task i.e. it is
the gap between the task finishing and it starting againAPIException
- if unable to schedule periodic task.Copyright © 2020 Push Technology Ltd. All Rights Reserved.