@Componentpublic class StatusListener implements ServletContextAware {
final ReentrantLock lock = new ReentrantLock();
@Override
public void setServletContext(ServletContext servletContext) {
Runnable runnable = new Runnable() {
public void run() {
statusTask();
}
};
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
// 第二个参数为首次执行的延时时间,第三个参数为定时执行的间隔时间
service.scheduleAtFixedRate(runnable, 20, 60, TimeUnit.SECONDS);
}
public void statusTask() {
final ReentrantLock lock = this.lock;
lock.lock();
try {
System.out.println(Thread.currentThread().getName());
System.out.println(Thread.currentThread().getId());
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
运行结果为:pool-2-thread-140
pool-4-thread-142
添加回答
举报
0/150
提交
取消