timer demo 之一

关于定时器的简单例子

Timer_demo.java

public class Timer_demo {
private Timer_demo demo = null;
private final Timer timer = new Timer();

private int minutes;

public Timer_demo(int minutes) {
this.minutes = minutes;
}
public void setObject(Timer_demo demo )
{
this.demo = demo;
}

public void start() {
timer.schedule(new TimerTask() {

public void run() {
playSound();
//demo.minutes+=1;
demo.start();

}

private void playSound()
{
System.out.println("Your egg is ready!"+new Date().toString());
}

}, minutes * 10 * 1000);
}

/**
* @param args
*/
public static void main(String[] args)
{
Timer_demo demo = new Timer_demo(1);
demo.setObject(demo);
demo.start();

}

阅读更多

三个问题之一:TIMER的问题!!!

07-29

我写的一个秒表程序,整体可以运行,暂停,重置都没问题,rnrn但是秒表运行部分有点问题,显示到了30秒,也就是30000毫秒rnrn以后,刷新就间隔很长,大概一秒多刷新一次最近时间。rnrn秒表计时部分的程序如下:rnrnstatic void run_stopwatch(stopwatch *pMe)rnrn char szChar[100];rn int i;rn rn m_ms=GETTIMEMS() - (pMe->lasttime);rn m_tms=m_ms/10;rnrn if(m_tms>=100)rn rn m_ss=m_tms/100;rn m_tms=m_tms%100;rn if(m_ss>=60)rn rn m_mm=m_ss/60;rn m_ss=m_ss%60;rn rn rnrn SPRINTF(szChar,"%.2d:%.2d:",m_mm,m_ss);rn SPRINTF(szChar,"%s%.2d",szChar,m_tms);rnrn for(i=0;szChar[i]!='\0';i++)rn rn *(szText+i)=*(szChar+i);rn rn *(szText+i)='\0';rnrn //IDisplay_ClearScreen(pMe->a.m_pIDisplay);rn IDisplay_DrawText(pMe->a.m_pIDisplay,rn AEE_FONT_NORMAL,rn szText,rn -1,rn 0,rn 0, rn NULL,rn IDF_ALIGN_CENTER |IDF_ALIGN_MIDDLE);rnrnrn ISHELL_CancelTimer(pMe->a.m_pIShell,(PFNNOTIFY)loop_stopwatch,pMe);rn ISHELL_SetTimer(pMe->a.m_pIShell,20,(PFNNOTIFY)loop_stopwatch,pMe);rn IDisplay_Update (pMe->a.m_pIDisplay); rnrnrnlasttime是在结构体中定义的一个全局变量,用于存储秒表刚开始的时间。rn其他变量定义如下:rnuint32 m_tms=0; //十毫秒 [0-99]rnuint32 m_ss=0; //秒 [0-59]rnuint32 m_mm=0; //分 [0-59]rnuint32 m_ms=0; //毫秒[0-最大值]rnAECHAR szText[100]; //显示时间格式的字符串MM:SS:MSrnrn请高手解答,为什么到30秒以后,刷新就要过1秒多.rn

没有更多推荐了,返回首页