当我有套接字和线程时如何彻底停止tomcat
目前,我通过 ServletContextListerner 启动一些线程,例如调度程序和套接字服务器,例如 mina。但此后tomcat无法正常关闭。我应该做什么释放套接字或杀死线程。
public class ServerListener implements ServletContextListener{
public void contextDestroyed(ServletContextEvent arg0){
//what should i do here
}
public void contextInitialized(ServletContextEvent arg0){
new Thread(new Runnable(){
public void run(){
SocketMain.main(null);
}
}).start();
new Thread(new Runnable(){
public void run(){
SccheduleMain.main(null);
}
}).start();
}
}
Currently i start some threads such scheduler and socketserver such as mina via to ServletContextListerner. But after this tomcat can not be shutdown correctly. What should i do release the socket or kill the thread .
public class ServerListener implements ServletContextListener{
public void contextDestroyed(ServletContextEvent arg0){
//what should i do here
}
public void contextInitialized(ServletContextEvent arg0){
new Thread(new Runnable(){
public void run(){
SocketMain.main(null);
}
}).start();
new Thread(new Runnable(){
public void run(){
SccheduleMain.main(null);
}
}).start();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先使用thread.setDaemon(true)告诉JVM将线程设置为守护线程。那么它将不会阻止 Tomcat 关闭。
First of all use
thread.setDaemon(true)
to tell the JVM to make the thread a daemon thread. Then it will NOT prevent Tomcat to shutdown.