ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(new echoServer(), 0, times, TimeUnit.MILLISECONDS);// 单位毫秒
static class echoServer implements Runnable {
@Override
public void run() {
// 要定时执行的任务
}
}
将以上代码写入到Servlet中,然后在web.xml中进行配置:
This is the description of my J2EE component
This is the display name of my J2EE component
Servlet名称
Servlet路径
5
Servlet名称
/Servlet访问路径
其中load-on-start 设置在项目启动时进行加载,值为正数,正数值越小,优先级越高