简单timer工具的实现步骤:
1.创建一个ITimeOut接口 ,用于实现类在TimeOut的处理。
public interface ITimeOut {
public void timeOut(int timerId, int remainCount);
}
2.创建Schedule
public class Schedule {
/* timer id */
private int timerId = 0;
/* temp variable with delay time*/
private long count = 0;
/* delay time(unit:second) */
private long delay = 0;
/* ITimeOut */
private ITimeOut iTimeOut = null;
private boolean reply = false;
/* reply times */
private volatile int replyTimes = -1;
/**
*
* @param timerId
* @param delay
* @param iTimeOut
* @param reply
* @param replyTimes
*/
public Schedule(int timerId, long delay, ITimeOut iTimeOut,
boolean reply, int replyTimes) {
this.timerId = timerId;
this.delay = delay;
this.iTimeOut = iTimeOut;
this.reply = reply;
this.count = delay;
this.replyTimes = replyTimes;
if (replyTimes != -1) {
this.replyTimes++;
}
}
/**
* reduce delay time
* @return
*/
public long reduceCount() {
long result = --count;
return result;
}
/**
* restart timer
*
* @return replay times
*/
public int reStart() {
if (replyTimes > 0) {
// reduce reply times
replyTimes--;
// reset delay time
count = delay;
}
return replyTimes;
}
public long getCount() {
return count;
}
public void setCount(long count) {
this.count = count;
}
public int getReplyTimes() {
return replyTimes;
}
public void setReplyTimes(int replyTimes) {
this.replyTimes = replyTimes;
}
public void setDelay(long delay) {
this.delay = delay;
this.count = delay;
}
public void setTimerId(int timerId) {
this.timerId = timerId;
}
public void setITimeOut(ITimeOut iTimeOut) {
this.iTimeOut = iTimeOut;
}
public void setReply(boolean reply) {
this.reply = reply;
}
public long getDelay() {
return delay;
}
public int getTimerId() {
return timerId;
}
public ITimeOut getITimeOut() {
return iTimeOut;
}
public boolean getReply() {
return reply;
}
}
3.ScheduleTable
public class ScheduleTable {
private Vector<Schedule> scheduleList = new Vector<Schedule>();
private volatile int maxTimerId = 0;
public ScheduleTable() {
}
public void addSchedule(Schedule schedule) {
synchronized (scheduleList) {
scheduleList.add(schedule);
}
}
/**
* get a unique timer id (max value is Integer max value)
*
* @return
*/
public int getNewTimerId() {
synchronized (scheduleList) {
int timerId = maxTimerId + 1;
Schedule schedule = null;
boolean loopFlag = true;
while (loopFlag) {
loopFlag = false;
for (int i = 0; i < this.size(); i++) {
schedule = (Schedule) scheduleList.get(i);
//check the timer id isn't exist
if (schedule.getTimerId() == timerId) {
if (timerId == Integer.MAX_VALUE) {
timerId = 1;
} else {
timerId++;
}
loopFlag = true;
break;
}
}
}
maxTimerId = timerId;
return timerId;
}
}
public boolean remove(Schedule schedule) {
synchronized (scheduleList) {
return scheduleList.removeElement(schedule);
}
}
public Schedule remove(int timerId) {
synchronized (scheduleList) {
Schedule schedule = this.findSchedule(timerId);
if (schedule != null) {
scheduleList.removeElement(schedule);
}
return schedule;
}
}
public int size() {
synchronized (scheduleList) {
return scheduleList.size();
}
}
public Schedule findSchedule(int timerId) {
synchronized (scheduleList) {
Schedule schedule = null;
for (int i = 0; i < this.size(); i++) {
schedule = (Schedule) scheduleList.get(i);
if (schedule.getTimerId() == timerId) {
return schedule;
}
}
return null;
}
}
public Schedule set(int index, Schedule schedule) {
synchronized (scheduleList) {
return (Schedule) scheduleList.set(index, schedule);
}
}
public Schedule getSchedule(int index) {
synchronized (scheduleList) {
if (index < scheduleList.size()) {
return (Schedule) scheduleList.get(index);
} else {
return null;
}
}
}
/**
* reduce delay time
*
* @param index
* @return
*/
public long reduceSchedule(int index) {
synchronized (scheduleList) {
return this.getSchedule(index).reduceCount();
}
}
}
4.ExecuteSchedule
public class ExecuteSchedule extends TimerTask {
/* schedule table */
private ScheduleTable scheduleTable = new ScheduleTable();
public ExecuteSchedule() {
}
/**
* set timer by schedule
*
* @param delay
* @param iTimeOut
* @param reply
* @param replyCount
* @return
*/
public int setTimer(long delay, ITimeOut iTimeOut,
boolean reply, int replyCount) {
synchronized (scheduleTable) {
//generate a unique id
int timerId = scheduleTable.getNewTimerId();
//add schedule to schedule table
scheduleTable.addSchedule(new Schedule(timerId, delay,
iTimeOut, reply, replyCount));
return timerId;
}
}
/**
* remote timer by timerId
*
* @param timerId
*/
public void clearTimer(int timerId) {
synchronized (scheduleTable) {
//from schedule table remove timer by timer id
scheduleTable.remove(timerId);
}
}
/**
* run schedule table
*/
public void run() {
synchronized (scheduleTable) {
for (int i = scheduleTable.size() - 1; i >= 0; i--) {
//delay time arrive
if (scheduleTable.reduceSchedule(i) == 0) {
Schedule schedule = scheduleTable.getSchedule(i);
if (schedule != null) {
int timerId = schedule.getTimerId();
int remainCount = schedule.reStart();
// reply completed when remain count equals 0
if (remainCount == 0) {
// remove timer by timer id
scheduleTable.remove(timerId);
}
ITimeOut iTimeOut = schedule.getITimeOut();
// time out process
if (iTimeOut != null) {
try {
if (iTimeOut != null) {
TimeOutHandlerThread thread = new TimeOutHandlerThread(
iTimeOut, timerId,
remainCount);
thread.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
}
}
5.TimeOutHandlerThread
class TimeOutHandlerThread extends Thread {
ITimeOut iTimeOut = null;
int timerId = -1;
int remainCount = 0;
public TimeOutHandlerThread(ITimeOut iTimeOut, int timerId,
int remainCount) {
super();
this.iTimeOut = iTimeOut;
this.timerId = timerId;
this.remainCount = remainCount;
}
public void run() {
try {
iTimeOut.timeOut(timerId, remainCount);
} catch (Exception e) {
e.printStackTrace();
}
}
}
6.TimerManager
public class TimerManager {
private static TimerManager timerManager = new TimerManager();
static{
if(timerManager != null){
timerManager.start();
}
}
private Timer timer = null;
private boolean isStarted = false;
private ExecuteSchedule executeSchedule = null;
private TimerManager() {
timer = new Timer();
executeSchedule = new ExecuteSchedule();
}
public int setTimer(long delay, ITimeOut iTimeOut,
boolean reply, int replyTimes) {
return executeSchedule.setTimer(delay, iTimeOut, reply,
replyTimes);
}
public int setTimer(long delay, ITimeOut iTimeOut,
int replyTimes) {
return executeSchedule.setTimer(delay, iTimeOut, true,
replyTimes);
}
public void clearTimer(int timerId) {
executeSchedule.clearTimer(timerId);
}
public static TimerManager getInstance() {
return timerManager;
}
public synchronized void start() {
if (this.isStarted) {
return;
}
timer.schedule(executeSchedule, 0, 1000);
this.isStarted = true;
}
public synchronized void stop() {
if (this.isStarted) {
timer.cancel();
this.isStarted = false;
}
}
}
使用该工具Demo
1.ITimeOut 实现类TimerTestHandler
public class TimerTestHandler implements ITimeOut {
TimerManager timerManager = TimerManager.getInstance();
public int startTimer(){
int timerId = timerManager.setTimer(10, this, false, 0);
return timerId;
}
public void stopTimer(int timerId){
timerManager.clearTimer(timerId);
}
public void timeOut(int timerId, int remainCount) {
System.out.println("TimeId = " + timerId + ", RemainCount = " + remainCount);
}
}
2.Main类
public class MyTestTimer {
public static void main(String[] args) throws Exception {
TimerTestHandler timerTestHandler = new TimerTestHandler();
timerTestHandler.startTimer();
}
}
1.创建一个ITimeOut接口 ,用于实现类在TimeOut的处理。
public interface ITimeOut {
public void timeOut(int timerId, int remainCount);
}
2.创建Schedule
public class Schedule {
/* timer id */
private int timerId = 0;
/* temp variable with delay time*/
private long count = 0;
/* delay time(unit:second) */
private long delay = 0;
/* ITimeOut */
private ITimeOut iTimeOut = null;
private boolean reply = false;
/* reply times */
private volatile int replyTimes = -1;
/**
*
* @param timerId
* @param delay
* @param iTimeOut
* @param reply
* @param replyTimes
*/
public Schedule(int timerId, long delay, ITimeOut iTimeOut,
boolean reply, int replyTimes) {
this.timerId = timerId;
this.delay = delay;
this.iTimeOut = iTimeOut;
this.reply = reply;
this.count = delay;
this.replyTimes = replyTimes;
if (replyTimes != -1) {
this.replyTimes++;
}
}
/**
* reduce delay time
* @return
*/
public long reduceCount() {
long result = --count;
return result;
}
/**
* restart timer
*
* @return replay times
*/
public int reStart() {
if (replyTimes > 0) {
// reduce reply times
replyTimes--;
// reset delay time
count = delay;
}
return replyTimes;
}
public long getCount() {
return count;
}
public void setCount(long count) {
this.count = count;
}
public int getReplyTimes() {
return replyTimes;
}
public void setReplyTimes(int replyTimes) {
this.replyTimes = replyTimes;
}
public void setDelay(long delay) {
this.delay = delay;
this.count = delay;
}
public void setTimerId(int timerId) {
this.timerId = timerId;
}
public void setITimeOut(ITimeOut iTimeOut) {
this.iTimeOut = iTimeOut;
}
public void setReply(boolean reply) {
this.reply = reply;
}
public long getDelay() {
return delay;
}
public int getTimerId() {
return timerId;
}
public ITimeOut getITimeOut() {
return iTimeOut;
}
public boolean getReply() {
return reply;
}
}
3.ScheduleTable
public class ScheduleTable {
private Vector<Schedule> scheduleList = new Vector<Schedule>();
private volatile int maxTimerId = 0;
public ScheduleTable() {
}
public void addSchedule(Schedule schedule) {
synchronized (scheduleList) {
scheduleList.add(schedule);
}
}
/**
* get a unique timer id (max value is Integer max value)
*
* @return
*/
public int getNewTimerId() {
synchronized (scheduleList) {
int timerId = maxTimerId + 1;
Schedule schedule = null;
boolean loopFlag = true;
while (loopFlag) {
loopFlag = false;
for (int i = 0; i < this.size(); i++) {
schedule = (Schedule) scheduleList.get(i);
//check the timer id isn't exist
if (schedule.getTimerId() == timerId) {
if (timerId == Integer.MAX_VALUE) {
timerId = 1;
} else {
timerId++;
}
loopFlag = true;
break;
}
}
}
maxTimerId = timerId;
return timerId;
}
}
public boolean remove(Schedule schedule) {
synchronized (scheduleList) {
return scheduleList.removeElement(schedule);
}
}
public Schedule remove(int timerId) {
synchronized (scheduleList) {
Schedule schedule = this.findSchedule(timerId);
if (schedule != null) {
scheduleList.removeElement(schedule);
}
return schedule;
}
}
public int size() {
synchronized (scheduleList) {
return scheduleList.size();
}
}
public Schedule findSchedule(int timerId) {
synchronized (scheduleList) {
Schedule schedule = null;
for (int i = 0; i < this.size(); i++) {
schedule = (Schedule) scheduleList.get(i);
if (schedule.getTimerId() == timerId) {
return schedule;
}
}
return null;
}
}
public Schedule set(int index, Schedule schedule) {
synchronized (scheduleList) {
return (Schedule) scheduleList.set(index, schedule);
}
}
public Schedule getSchedule(int index) {
synchronized (scheduleList) {
if (index < scheduleList.size()) {
return (Schedule) scheduleList.get(index);
} else {
return null;
}
}
}
/**
* reduce delay time
*
* @param index
* @return
*/
public long reduceSchedule(int index) {
synchronized (scheduleList) {
return this.getSchedule(index).reduceCount();
}
}
}
4.ExecuteSchedule
public class ExecuteSchedule extends TimerTask {
/* schedule table */
private ScheduleTable scheduleTable = new ScheduleTable();
public ExecuteSchedule() {
}
/**
* set timer by schedule
*
* @param delay
* @param iTimeOut
* @param reply
* @param replyCount
* @return
*/
public int setTimer(long delay, ITimeOut iTimeOut,
boolean reply, int replyCount) {
synchronized (scheduleTable) {
//generate a unique id
int timerId = scheduleTable.getNewTimerId();
//add schedule to schedule table
scheduleTable.addSchedule(new Schedule(timerId, delay,
iTimeOut, reply, replyCount));
return timerId;
}
}
/**
* remote timer by timerId
*
* @param timerId
*/
public void clearTimer(int timerId) {
synchronized (scheduleTable) {
//from schedule table remove timer by timer id
scheduleTable.remove(timerId);
}
}
/**
* run schedule table
*/
public void run() {
synchronized (scheduleTable) {
for (int i = scheduleTable.size() - 1; i >= 0; i--) {
//delay time arrive
if (scheduleTable.reduceSchedule(i) == 0) {
Schedule schedule = scheduleTable.getSchedule(i);
if (schedule != null) {
int timerId = schedule.getTimerId();
int remainCount = schedule.reStart();
// reply completed when remain count equals 0
if (remainCount == 0) {
// remove timer by timer id
scheduleTable.remove(timerId);
}
ITimeOut iTimeOut = schedule.getITimeOut();
// time out process
if (iTimeOut != null) {
try {
if (iTimeOut != null) {
TimeOutHandlerThread thread = new TimeOutHandlerThread(
iTimeOut, timerId,
remainCount);
thread.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
}
}
5.TimeOutHandlerThread
class TimeOutHandlerThread extends Thread {
ITimeOut iTimeOut = null;
int timerId = -1;
int remainCount = 0;
public TimeOutHandlerThread(ITimeOut iTimeOut, int timerId,
int remainCount) {
super();
this.iTimeOut = iTimeOut;
this.timerId = timerId;
this.remainCount = remainCount;
}
public void run() {
try {
iTimeOut.timeOut(timerId, remainCount);
} catch (Exception e) {
e.printStackTrace();
}
}
}
6.TimerManager
public class TimerManager {
private static TimerManager timerManager = new TimerManager();
static{
if(timerManager != null){
timerManager.start();
}
}
private Timer timer = null;
private boolean isStarted = false;
private ExecuteSchedule executeSchedule = null;
private TimerManager() {
timer = new Timer();
executeSchedule = new ExecuteSchedule();
}
public int setTimer(long delay, ITimeOut iTimeOut,
boolean reply, int replyTimes) {
return executeSchedule.setTimer(delay, iTimeOut, reply,
replyTimes);
}
public int setTimer(long delay, ITimeOut iTimeOut,
int replyTimes) {
return executeSchedule.setTimer(delay, iTimeOut, true,
replyTimes);
}
public void clearTimer(int timerId) {
executeSchedule.clearTimer(timerId);
}
public static TimerManager getInstance() {
return timerManager;
}
public synchronized void start() {
if (this.isStarted) {
return;
}
timer.schedule(executeSchedule, 0, 1000);
this.isStarted = true;
}
public synchronized void stop() {
if (this.isStarted) {
timer.cancel();
this.isStarted = false;
}
}
}
使用该工具Demo
1.ITimeOut 实现类TimerTestHandler
public class TimerTestHandler implements ITimeOut {
TimerManager timerManager = TimerManager.getInstance();
public int startTimer(){
int timerId = timerManager.setTimer(10, this, false, 0);
return timerId;
}
public void stopTimer(int timerId){
timerManager.clearTimer(timerId);
}
public void timeOut(int timerId, int remainCount) {
System.out.println("TimeId = " + timerId + ", RemainCount = " + remainCount);
}
}
2.Main类
public class MyTestTimer {
public static void main(String[] args) throws Exception {
TimerTestHandler timerTestHandler = new TimerTestHandler();
timerTestHandler.startTimer();
}
}