如何设计一个类不停的循环检查!
我开始这样写,可能是占资源过多,无法处理.
while(true){
System.out.println("do something ");
try{
if(systemQueue.getMsgQueueSize()!=0){
//有东西要处理,处理,进入下一次循环
mainFrame.MainLoop();
System.out.println("process topic ");
}else{
//没东西处理,等一秒,进入下一次循环
System.out.println("process wait ");
Thread.sleep(1);
}
}catch(Exception ex){
ex.printStackTrace();
}
}
设置成Thread.sleep(1000)可以处理了.
但是要求实时处理.
各位有什么好建议.
我后来改成public class Timetask {
static int i=1;
int index=0;
Timer timer;
public Timetask() {
i++;
//System.out.println("listener:"+i);
timer = new Timer();
timer.schedule(new DatumTimer(),0,1);// 每miao激发一次
}
class DatumTimer extends TimerTask {
public void run()
{
try {
System.out.println("do something ");
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
也不行