
org.simpleframework.util.thread.Scheduler Maven / Gradle / Ivy
/*
* Scheduler.java February 2007
*
* Copyright (C) 2007, Niall Gallagher
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
*/
package org.simpleframework.util.thread;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
/**
* The Scheduler
object is used to schedule tasks for
* execution. This queues the task for the requested period of time
* before it is executed. It ensures that the delay is adhered to
* such that tasks can be timed for execution in an accurate way.
*
* @author Niall Gallagher
*/
public class Scheduler implements Executor {
/**
* This is the scheduler queue used to enque tasks to execute.
*/
private SchedulerQueue engine;
/**
* Constructor for the Scheduler
object. This will
* create a scheduler with a fixed number of threads to use
* before execution. Depending on the types of task that are
* to be executed this should be increased for accuracy.
*
* @param size this is the number of threads for the scheduler
*/
public Scheduler(int size) {
this.engine = new SchedulerQueue(size);
}
/**
* This will execute the task within the executor immediately
* as it uses a delay duration of zero milliseconds. This can
* be used if the scheduler is to be used as a thread pool.
*
* @param task this is the task to schedule for execution
*/
public void execute(Runnable task) {
execute(task, 0);
}
/**
* This will execute the task within the executor after the time
* specified has expired. If the time specified is zero then it
* will be executed immediately. Once the scheduler has been
* stopped then this method will no longer accept runnable tasks.
*
* @param task this is the task to schedule for execution
* @param delay the time in milliseconds to wait for execution
*/
public void execute(Runnable task, long delay) {
execute(task, delay, TimeUnit.MILLISECONDS);
}
/**
* This will execute the task within the executor after the time
* specified has expired. If the time specified is zero then it
* will be executed immediately. Once the scheduler has been
* stopped then this method will no longer accept runnable tasks.
*
* @param task this is the task to schedule for execution
* @param delay this is the delay to wait before execution
* @param unit this is the duration time unit to wait for
*/
public void execute(Runnable task, long delay, TimeUnit unit) {
engine.schedule(task, delay, unit);
}
/**
* This is used to stop the executor by interrupting all running
* tasks and shutting down the threads within the pool. This will
* return immediately once it has been stopped, and not further
* tasks will be accepted by this pool for execution.
*/
public void stop() {
engine.stop();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy