public class TimerTest {
private static final Logger logger = Logger.getLogger(TimerTest.class.getSimpleName());
@Test
public void test() throws IOException {
Timer timer = new Timer(true);
timer.schedule(new TimerTask() {
@Override
public void run() {
logger.info("time: " + System.currentTimeMillis());
}
}, 2000, 4000); // 2000表示第一次执行任务延迟时间,40表示以后每隔多长时间执行一次run里面的任务
System.in.read();
}
}
public class ScheduledThreadPoolExecutorTest {
private static final Logger logger = Logger.getLogger(ScheduledThreadPoolExecutorTest.class.getSimpleName());
@Test
public void test() throws IOException {
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(2);
executor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
logger.info("time: " + System.currentTimeMillis());
}
}, 2000, 40, TimeUnit.MILLISECONDS); // 2000表示首次执行任务的延迟时间,40表示每次执行任务的间隔时间,TimeUnit.MILLISECONDS执行的时间间隔数值单位
System.in.read();
}
}