1 Reply Latest reply on Dec 23, 2011 1:59 PM by gimbal2

    Starting a new thread in Servlet

      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.

        • 1. Re: Starting a new thread in Servlet
          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.