📄 advthread.java
字号:
package com.jmobilecore.thread;
/**
* This class implements scheduling mechanism for repeatable task execution
* You can specify frequency of execution and delay before first start.
* Also you can temporaryly stop and resume task execution or manually run
* the specified task
*
* @author Greg Gridin
*/
public class AdvThread extends Thread {
/**
* The task for current threas
*/
public Runnable task;
/**
* Indicates if current thread is in active phase
*/
volatile protected boolean isActive = false;
/**
* Indicates if current task is forces to run
*
* @see #runOnce
*/
volatile protected boolean forcedRun = false;
/**
* Indicates if the frequency was reset
*
* @see #setFrequency(int frequency)
* @see #setFrequency(int frequency, int delay)
*/
volatile protected boolean reset = true;
/**
* The frequency (in milliseconds) of repeatable task execution
*
* @see #task
*/
volatile protected int frequency = 0;
/**
* The delay (in milliseconds) before first execution of current task
*/
volatile protected int delay = 0;
/**
* Creates a new <code>AdvThread</code> instance for specified task.
*
* @param task The Runnable object containing task implementation.
* Task can be scheduled for repeatable or single tame execution later
*/
public AdvThread(Runnable task) {
super();
this.task = task;
forcedRun = true;
}
/**
* Creates a new <code>AdvThread</code> instance for specified task
* and specifies frequency for repeatable execution.
*
* @param task The Runnable object containing task implementation.
* @param frequency The frequency in milliseconds for repetable task execution
* Frequency should be non-negative integer, ignored otherwise
* If frequency is zero then automatic execution is deisabled, task could be excecuted by runOnce method only
*/
public AdvThread(Runnable task, int frequency) {
super();
this.task = task;
this.frequency = frequency;
}
/**
* Set frequency for automatic execution of thread
*
* @param frequency The frequency in milliseconds for repeatable task execution
* If frequency is zero then automatic execution is deisabled, task could be excecuted by runOnce method only
* First execution of task will happen after frequency delay
*/
synchronized public boolean setFrequency(int frequency) {
return setFrequency(frequency, 0);
}
/**
* Set frequency for automatic execution of thread
*
* @param frequency The frequency in milliseconds for repeatable task execution
* If frequency is zero then automatic execution is deisabled, task could be excecuted by runOnce method only
* First execution of task will happen after frequency delay
* @param delay The delay in milliseconds of first task execution
*/
synchronized public boolean setFrequency(int frequency, int delay) {
if (!isActive)
return false;
if (frequency < 0)
return false;
this.frequency = frequency;
this.delay = delay;
forcedRun = false;
if (this.frequency != 0) {
reset = true;
notify();
}
return true;
}
/**
* Terminates thread processing
* <p/>
* If task is running then it will continue to run, when completed no other executions will happen
* if task is scheduled for execution, it will be cancelled
*/
synchronized public boolean terminate() {
if (!isActive)
return false;
isActive = false;
notify();
return true;
}
/**
* Manual execution of task
* <p/>
* If task is running then it will continue to run, when completed no other executions will happen
* Next task launch is not affected unless task execution is not completed at the time of scheduled launch
* At this case next task launch is skipped
*/
synchronized public boolean runOnce() {
if (!isActive)
return false;
forcedRun = true;
notify();
return true;
}
/**
* Overrides standard run method of Thread class
* This methods works as scheduler for <code>Runnable task</code> execution
* @see #task
*/
public void run() {
if (task == null)
return;
isActive = true;
while (isActive) {
try {
synchronized (this) {
if (frequency > 0) {
wait(frequency - System.currentTimeMillis() % frequency + delay);
}
if (frequency <= 0 && !forcedRun) {
wait();
}
}
if (forcedRun) {
reset = forcedRun = false;
delay = 0;
} else {
if (reset) {
reset = false;
delay = 0;
continue;
}
}
} catch (Exception e) {
continue;
}
if (isActive && task != null) {
task.run();
}
}
destructor();
}
/**
* Default destructor. Helps VM to perform garbage collection
*/
protected void destructor() {
isActive = false;
task = null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -