实现的方法有两种不过都是继承于线程类来实现的。1。 public void run() { while (true) { try { sleep(10000L); //间隔10s执行一次! } catch (Exception ex) { ToolBox.getLogger().error("Executing the sleep operation error!"); ex.printStackTrace(); } 2。采用timer类和timertask类配合实现 在timertask里面有一个时间间隔执行任务的方法schedule的方法可以达到你的要求! OperationExecute(); } }
实现的方法有两种不过都是继承于线程类来实现的。1。 public void run() { while (true) { try { sleep(10000L); //间隔10s执行一次! } catch (Exception ex) { ToolBox.getLogger().error("Executing the sleep operation error!"); ex.printStackTrace(); } OperationExecute();//你的操作 } } 2。采用Timer 类和TimerTask 类配合实现 在Timer 里面有一个时间间隔执行任务的方法schedule的方法可以达到你的要求!Timer tim = new Timer(); TimerTask tTask = new TimerTask(); //在里面实现你的操作 tim.schedule(tTask,0,10000);//间隔时间10000你自己设定
同意楼上的,TimerTask就是干这个的.
public class YourTask extends TimerTask { public void run(){ //每个一段时间你想要做的事 } }Timer timer = new Timer(); YourTask task = new YourTask(); timer.scheduleAtFixedRate(task,开始时间,周期);//开始时间Date型,周期long型豪秒级
public void run() {
while (true) {
try {
sleep(10000L); //间隔10s执行一次!
}
catch (Exception ex) {
ToolBox.getLogger().error("Executing the sleep operation error!");
ex.printStackTrace();
}
2。采用timer类和timertask类配合实现
在timertask里面有一个时间间隔执行任务的方法schedule的方法可以达到你的要求!
OperationExecute();
}
}
public void run() {
while (true) {
try {
sleep(10000L); //间隔10s执行一次!
}
catch (Exception ex) {
ToolBox.getLogger().error("Executing the sleep operation error!");
ex.printStackTrace();
}
OperationExecute();//你的操作
}
} 2。采用Timer 类和TimerTask 类配合实现
在Timer 里面有一个时间间隔执行任务的方法schedule的方法可以达到你的要求!Timer tim = new Timer();
TimerTask tTask = new TimerTask(); //在里面实现你的操作
tim.schedule(tTask,0,10000);//间隔时间10000你自己设定
public void run(){
//每个一段时间你想要做的事
}
}Timer timer = new Timer();
YourTask task = new YourTask();
timer.scheduleAtFixedRate(task,开始时间,周期);//开始时间Date型,周期long型豪秒级