Package org.apache.wicket.util.thread
Class Task
- java.lang.Object
-
- org.apache.wicket.util.thread.Task
-
public final class Task extends Object
Runs a block of code periodically. ATaskcan be started at a given time in the future and can be a daemon. The block of code will be passed aLogobject each time it is run through itsICodeinterface.If the code block takes longer than the period to run, the next task invocation will occur immediately. In this case, tasks will not occur at precise multiples of the period. For example, if you run a task every 30 seconds, and the first run takes 40 seconds but the second takes 20 seconds, your task will be invoked at 0 seconds, 40 seconds and 70 seconds (40 seconds + 30 seconds), which is not an even multiple of 30 seconds.
In general, this is a simple task class designed for polling activities. If you need precise guarantees, you probably should be using a different task class.
- Since:
- 1.2.6
- Author:
- Jonathan Locke
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected org.slf4j.LoggergetLog()Gets the log for thisTask.intgetPriority()Gets the thread priorityvoidinterrupt()Interrupts theTaskas soon as it has the opportunity.voidrun(Duration frequency, ICode code)Runs thisTaskat the given frequency.voidsetDaemon(boolean daemon)Sets daemon or not.voidsetLog(org.slf4j.Logger log)Sets log for user code to log to when task runs.voidsetPriority(int prio)Sets the priority of the threadvoidsetStartTime(Instant startTime)Sets start time for this task.voidstop()Stops thisTaskas soon as it has the opportunity.StringtoString()
-
-
-
Method Detail
-
run
public final void run(Duration frequency, ICode code)
Runs thisTaskat the given frequency. You may only call this method if the task has not yet been started. If the task is already running, anIllegalStateExceptionwill be thrown.- Parameters:
frequency- the frequency at which to run the codecode- the code to run- Throws:
IllegalStateException- thrown if task is already running
-
setDaemon
public void setDaemon(boolean daemon)
Sets daemon or not. For obvious reasons, this value can only be set before the task starts running. If you attempt to set this value after the task starts running, anIllegalStateExceptionwill be thrown.- Parameters:
daemon-trueif thisTask'sThreadshould be a daemon- Throws:
IllegalStateException- thrown if task is already running
-
setLog
public void setLog(org.slf4j.Logger log)
Sets log for user code to log to when task runs.- Parameters:
log- the log
-
setStartTime
public void setStartTime(Instant startTime)
Sets start time for this task. You cannot set the start time for a task which is already running. If you attempt to, an IllegalStateException will be thrown.- Parameters:
startTime- The time this task should start running- Throws:
IllegalStateException- Thrown if task is already running
-
toString
public String toString()
- Overrides:
toStringin classObject- See Also:
Object.toString()
-
getLog
protected org.slf4j.Logger getLog()
Gets the log for thisTask.- Returns:
- the log
-
stop
public void stop()
Stops thisTaskas soon as it has the opportunity.
-
interrupt
public void interrupt()
Interrupts theTaskas soon as it has the opportunity.
-
setPriority
public void setPriority(int prio)
Sets the priority of the thread- Parameters:
prio-
-
getPriority
public int getPriority()
Gets the thread priority- Returns:
- priority
-
-