public class ScheduledThreadPoolExecutorTest {
public static void main(String[] args) {
ScheduledThreadPoolExecutor executorService = new ScheduledThreadPoolExecutor(1);
BusinessTask task = new BusinessTask();
//1秒后开始执行任务,以后每隔2秒执行一次
executorService.scheduleWithFixedDelay(task, 1000, 2000,TimeUnit.MILLISECONDS);
}
private static class BusinessTask implements Runnable{
@Override
public void run() {
//捕获所有的异常,保证定时任务能够继续执行
try{
System.out.println("任务开始...");
//doBusiness();
System.out.println("任务结束...");
}catch (Throwable e) {
// donothing
}
}
}
}