9

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();

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?

Dónal
  • 176,670
  • 166
  • 541
  • 787

2 Answers2

9

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.

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.

gustavohenke
  • 38,209
  • 13
  • 113
  • 120
Anton
  • 5,365
  • 3
  • 28
  • 43
1

Here is a discussion about running thread from servlet.

http://www.jguru.com/faq/view.jsp?EID=455215

Another discussion is about running thread from an EJB container.

Java EE specification and multi threading

Community
  • 1
  • 1
Sujith Nair
  • 342
  • 3
  • 9