如何在JAVA中每隔一段时间执行一段程序

可以用线程来做,每隔几秒开一个线程
代码如下
public void runTask() {
final long timeInterval = 120000;// 两分钟运行一次
final ThreadService threadService = new ThreadService();
Runnable runnable = new Runnable() {
public void run() {
while (true) {
// ------- code for task to run
//你要运行的程序
// ------- ends here
try {
Thread.sleep(timeInterval);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread thread = new Thread(runnable);
thread.start();
}
上一篇:【C#-多线程】实现每隔一段时间执行代码(多线程) 3种定时器


下一篇:js每隔一段时间执行函数