在Tomcat下启动线程 [英] starting threads under Tomcat

查看:120
本文介绍了在Tomcat下启动线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人告诉我你不应该从在Tomcat下运行的webapp(或任何其他容器,大概是)开始自己的线程

Someone told me that you shouldn't start your own threads from a webapp running under Tomcat (or any other container, presumably)

Runnable myRunnable = new Runnable() {
  public void run() {
    System.out.println("I'm running");
  }
}

new Thread(myRunnable).start();

或类似地:

ScheduledThreadPoolExecutor retrySchedulerService = new ScheduledThreadPoolExecutor(3);
retrySchedulerService.schedule(dlrRetryTask, 120, TimeUnit.SECONDS);

除了上述任何一种,你应该从一些线程池请求一个线程Tomcat知道。这有什么道理,还是完全poppycock?

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有自己的thead池,用于处理传入的请求。我不认为使用它是个好主意,即使你设法访问它。

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.

一般来说,启动线程不是一个好习惯。一个Java EE环境,但在像Tomcat这样的servlet容器中启动线程没什么不好。

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.

这篇关于在Tomcat下启动线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆