, 여기에 다른 대답을 제공 .Daemons </리스너>
다음과 같은
ServletContextListener
의 구현으로 데몬 클래스를 구현합니다
t 그는 코드가 5 초마다 호출되며, Tomcat은 앱이 종료 될 때 contextDestroyed를 호출합니다. 이 변수는 휘발성이 있습니다. 그렇지 않으면 멀티 코어 시스템에서 시스템 종료시 문제가 발생할 수 있습니다.
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class Daemons implements ServletContextListener {
private volatile boolean active = true;
Runnable myDeamon = new Runnable() {
public void run() {
while (active) {
try {
System.out.println("checking changed files...");
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
public void contextInitialized(ServletContextEvent servletContextEvent) {
new Thread(myDeamon).start();
}
public void contextDestroyed(ServletContextEvent servletContextEvent) {
active = false;
}
}
문제는 알림 문제가 아니며, 대신 tomcat에서 데몬을 시작하는 가장 좋은 방법은 무엇입니까? 스레드를로드하는 서블릿을 사용할 수없는 이유는 무엇입니까? –