@ Component
public 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();
}
}
}
the running result is
pool-2-thread-1
40
pool-4-thread-1
42