Can I create a stream from a servlet?

I would like to ask the main question before moving on to my main question.

Lets say that I am running a simple Java program that spawns a thread in the main function. Will the thread continue to work when the main function completes? Is there a concept for the relationship between parents and children between threads.

I have a servlet that takes a long time to process the request (5 minutes). Can I create a background thread from the main servlet to handle the work and return soon. Will the background thread continue to work even when the main servlet completes processing?

+6
multithreading servlets
source share
2 answers
  • There is such a thing as parent and child threads, but you do not have much control over this. For example, there is InheritableThreadLocal , where you can store variables for the hierarchy of threads.

  • you can create a new thread from the servlet. Prefer Java 5 Artist Infrastructure

  • using servlet 3.0, view asynchronous processing .

+2
source share

If you want your application to exit, although you still have threads, you have to mark the thread as a daemon thread:

  Thread t = new Thread (myRunnable);
 t.setDaemon (true),
 t.start ();

This is especially important when you do this on the application server, otherwise the server cannot be disconnected!

If you repeat this, you can think of ThreadPool to make it more efficient.

+4
source share

All Articles