This discussion is archived
1 Reply Latest reply: Dec 23, 2011 5:59 AM by gimbal2 RSS

Starting a new thread in Servlet

shravan Newbie
Currently Being Moderated
Hi All,

Can we create a new thread in servlet?
How long will the execution happens( i.e., till service/doGet/doPost method ends or till thread is completed)?

My requirement is to when a request comes to servlet immediately it should send the some response. business logic should happen in some new thread.


If you know please share me the code snippet.

Thanks,
shravan.
  • 1. Re: Starting a new thread in Servlet
    gimbal2 Guru
    Currently Being Moderated
    Threads are always detached from the code that starts it and run completely isolated. You can start a thread, let the doGet()/doPost() end and the thread will continue on its merry way. Of course you won't be able to send response on what it is doing.

    The question is: should you start a thread in a servlet. The application server / servlet container can maintain its own thread pool for the services it provides (web services, EJBs, MDBs, etc.) and if you start spawning threads on your own you can interfere with it. You should research that a little about the server you are using, I know that under JBoss for example it is at the very least discouraged to do so.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points