threadcontrol.java
来自「开发j2me 手机程序必须的用到的。文件较小」· Java 代码 · 共 75 行
JAVA
75 行
package com.jmobilecore.thread;
public class ThreadControl {
/**
* The current thread
*/
public AdvThread thread;
public ThreadControl(Runnable task) {
setTask(task, 0);
}
public ThreadControl(Runnable task, int frequency) {
setTask(task, frequency);
}
/**
* Sets the current task and frequency of the task execution
* @param task the new <code>Runnable</code> task
* @param frequency the frequency of the task execution
* @return <code>true</code> if setup is successful, <code>false</code> otherwise
*/
protected boolean setTask(Runnable task, int frequency) {
if (frequency<0) return false;
thread = new AdvThread(task, frequency);
if (thread != null) {
thread.start();
return true;
}
return false;
}
/**
* Stops the current thread
*/
public boolean stop() {
return reset(0, 0);
}
/**
* Resets frecuency and delay for the current thread
* @param frequency the frequency of the task execution
* @param delay the delay before the first task execution
* @return <code>true</code> if reset is successful, <code>false</code> otherwise
*/
public boolean reset(int frequency, int delay) {
if (thread != null && thread.isAlive() ) {
return thread.setFrequency(frequency, delay);
}
return false;
}
/**
* Terminates the current thread
* @return <code>true</code> if termination is done, <code>false</code> otherwise
*/
public boolean terminate() {
if (thread != null && thread.isAlive() ) {
return thread.terminate();
}
return false;
}
/**
* Default destructor. Helps VM to perform garbage collection
*/
public void destructor() {
if ( !terminate() ) return;
thread = null;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?