java 在Tomcat下启动线程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10302349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
starting threads under Tomcat
提问by Dónal
Someone told me that you shouldn't start your own threads from a webapp running under Tomcat (or any other container, presumably)
有人告诉我,你不应该从在 Tomcat(或任何其他容器,大概)下运行的 webapp 启动你自己的线程
Runnable myRunnable = new Runnable() {
public void run() {
System.out.println("I'm running");
}
}
new Thread(myRunnable).start();
Or similarly:
或类似:
ScheduledThreadPoolExecutor retrySchedulerService = new ScheduledThreadPoolExecutor(3);
retrySchedulerService.schedule(dlrRetryTask, 120, TimeUnit.SECONDS);
Instead of either of the above, you're supposed to request a thread from some pool of threads that Tomcat knows about. Is there any truth to this, or is it utter poppycock?
您应该从 Tomcat 知道的一些线程池中请求一个线程,而不是上述任何一个。这有什么道理,还是完全是罂粟花?
回答by Anton
Feel free to start your own threads, but remember to stop them when the application stops. Tomcat got its own thead pool, which is used for handling incoming requests. I don't think that it's a good idea to use it, even if you manage to get access to it.
随意启动您自己的线程,但请记住在应用程序停止时停止它们。Tomcat 有自己的 thead 池,用于处理传入的请求。我不认为使用它是个好主意,即使您设法访问它。
Generally, it's not a good practice to start threads in a Java EE environment, but nothing bad in starting threads in a servlet container like Tomcat.
通常,在 Java EE 环境中启动线程不是一个好习惯,但在 Tomcat 等 servlet 容器中启动线程也没什么不好。
回答by Sujith Nair
Here is a discussion about running thread from servlet.
这是关于从 servlet 运行线程的讨论。
http://www.jguru.com/faq/view.jsp?EID=455215
http://www.jguru.com/faq/view.jsp?EID=455215
Another discussion is about running thread from an EJB container.
另一个讨论是关于从 EJB 容器运行线程。