📄 pub_myrunnable.java
字号:
package telproject;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
import java.lang.*;
/**
* 定义的虚拟类,本系统所有线程类要继承本类来实现
*/
public abstract class Pub_MyRunnable implements Runnable, List_userData {
public final static String SYSVER = "1.0";
protected boolean bStop = true; // 是否停止
protected Thread thread = null; // 当前线程
private long iLastTime = 0;
private String sThreadName;
private int iMaxIdle = 100; // 最大的不活动时间(秒)
public Pub_MyRunnable() {
sThreadName = this.toString();
}
public Pub_MyRunnable(int i) {
this();
setMaxIdle(i);
}
public Pub_MyRunnable(String s) {
sThreadName = s;
}
public Pub_MyRunnable(String s, int i) {
this(s);
setMaxIdle(i);
}
public void setMaxIdle(int i) {
iMaxIdle = i;
if (iMaxIdle <= 0) {
iMaxIdle = 120;
}
}
public String getName() {
return sThreadName;
}
public int getMaxIdle() {
return iMaxIdle;
}
/**
* thread是否可能已停止运行
*/
public boolean mayStop() {
if (getIdle() > getMaxIdle()) {
return true;
}
return false;
}
/**
* 启动线程
*/
public boolean start() {
bStop = false;
thread = new Thread(this);
thread.start();
iLastTime = System.currentTimeMillis();
return true;
}
public long getIdle() {
long l = (System.currentTimeMillis() - iLastTime) / 1000;
return l;
}
public void resetIdle() {
iLastTime = System.currentTimeMillis();
}
/**
* 停止线程
*/
public void stop() {
if (thread == null) {
return;
}
bStop = true;
boolean b = false;
int i = 0;
while (thread.isAlive() && i < 20) {//i = 50 modify to i = 20
try {
thread.sleep(10);
} catch (Exception e) {
}
i++;
}
if (thread.isAlive()) {
thread.stop();
}
thread = null;
end();
}
/**
* 线程停止后执行的用于清除变量/重新初试化参数
*/
protected abstract void end();
/**
* 线程休眠一段时间
*
* @param iTime
* 时间长度,以毫秒为单位
*/
public void sleep(int iTime) {
if (thread == null) {
return;
}
if (iTime < 0) {
return;
}
try {
thread.sleep(iTime);
} catch (Exception e) {
}
}
public Thread getThread() {
return thread;
}
public boolean isAlive() {
boolean b = false;
if (thread != null) {
b = thread.isAlive();
}
return b;
}
/**
* 线程是否已停止
*/
public boolean stoped() {
return bStop;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -