Package eu.woolplatform.utils.schedule
Class SerialJobRunner
- java.lang.Object
-
- eu.woolplatform.utils.schedule.SerialJobRunner
-
public class SerialJobRunner extends Object
This class can be used to run jobs sequentially on a separate thread. A job is only started after any previous job has been completed (including notification of the listener) or the previous job has been cancelled. The thread will only run as long as there are jobs in the queue.
-
-
Field Summary
Fields Modifier and Type Field Description static intPRIORITY_HIGHstatic intPRIORITY_LOWstatic intPRIORITY_MEDIUMstatic intPRIORITY_NONE
-
Constructor Summary
Constructors Constructor Description SerialJobRunner()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcancelJobs()Cancels all running and pending jobs.voidcancelJobs(Job... jobs)Cancels the specified jobs.voidcancelJobs(List<? extends Job> jobs)Cancels the specified jobs.List<Job>getJobs()Returns all jobs that have been posted and have not yet been completed.voidpostJob(Job job, int priority, JobListener listener)Posts a job that will be run after previously posted jobs with the same or higher priority, but before posted jobs with lower priority.voidpostJob(Job job, JobListener listener)Posts a job with priorityPRIORITY_MEDIUM.protected voidpostOnNotifyThread(Runnable runnable)Posts the specified runnable to be run on the notify thread.protected voidrunOnNotifyThread(Runnable runnable)Runs the specified runnable on the notify thread and waits until it is completed.
-
-
-
Field Detail
-
PRIORITY_NONE
public static final int PRIORITY_NONE
- See Also:
- Constant Field Values
-
PRIORITY_LOW
public static final int PRIORITY_LOW
- See Also:
- Constant Field Values
-
PRIORITY_MEDIUM
public static final int PRIORITY_MEDIUM
- See Also:
- Constant Field Values
-
PRIORITY_HIGH
public static final int PRIORITY_HIGH
- See Also:
- Constant Field Values
-
-
Method Detail
-
getJobs
public List<Job> getJobs()
Returns all jobs that have been posted and have not yet been completed. This includes the current job and pending jobs.- Returns:
- the jobs
-
postJob
public void postJob(Job job, JobListener listener)
Posts a job with priorityPRIORITY_MEDIUM.- Parameters:
job- the joblistener- the job listener or null
-
postJob
public void postJob(Job job, int priority, JobListener listener)
Posts a job that will be run after previously posted jobs with the same or higher priority, but before posted jobs with lower priority.You may use one of the PRIORITY_* constants defined in this class. A higher number indicates a higher priority.
- Parameters:
job- the jobpriority- the prioritylistener- the job listener or null
-
cancelJobs
public void cancelJobs()
Cancels all running and pending jobs.
-
cancelJobs
public void cancelJobs(Job... jobs)
Cancels the specified jobs.- Parameters:
jobs- the jobs
-
cancelJobs
public void cancelJobs(List<? extends Job> jobs)
Cancels the specified jobs.- Parameters:
jobs- the jobs
-
postOnNotifyThread
protected void postOnNotifyThread(Runnable runnable)
Posts the specified runnable to be run on the notify thread. In Android it will run on the UI thread. This method does not wait until the runnable is completed.- Parameters:
runnable- the runnable
-
runOnNotifyThread
protected void runOnNotifyThread(Runnable runnable)
Runs the specified runnable on the notify thread and waits until it is completed. In Android it will run on the UI thread. This method should always be called from a worker thread to avoid a deadlock.- Parameters:
runnable- the runnable
-
-