JAVA 定时器 ScheduledThreadPoolExecutor


...
//定时器
ScheduledThreadPoolExecutor monitorService = new ScheduledThreadPoolExecutor(10, new ThreadFactoryBuilder().setNameFormat("lifecycleSupervisor-" + Thread.currentThread().getId() + "-%d").build());
//最大20个线程
monitorService.setMaximumPoolSize(20);
//空闲30秒就收回
monitorService.setKeepAliveTime(30, TimeUnit.SECONDS);

ScheduledFuture future = monitorService.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
System.out.println(new Date());
}
}, 0, 3, TimeUnit.SECONDS);
...

你可能感兴趣的:(JAVA)