Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

starting threads under Tomcat [duplicate]

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?

like image 481
Dónal Avatar asked Apr 24 '12 16:04

Dónal


People also ask

Does Tomcat reuse threads?

After a context is stopped, threads in the pool are renewed.

Is Tomcat server multithreaded?

The original Tomcat implementation of the Coyote con- nector follows a pure multi-threaded approach to manage the client connections.

How many threads does Tomcat create?

By default, Tomcat sets maxThreads to 200, which represents the maximum number of threads allowed to run at any given time.

Is Tomcat single threaded?

By default, Tomcat allocates a single thread to perform deployment and management of applications. When multiple applications are handled by a single Tomcat instance, this can cause the startup time to increase considerably, as each application is started in sequence by the management thread.


1 Answers

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.

like image 59
Anton Avatar answered Sep 28 '22 08:09

Anton