Forum Stats

  • 3,855,697 Users
  • 2,264,546 Discussions


Starting a new thread in Servlet

Shravan-Oracle Member Posts: 27 Employee
edited Dec 23, 2011 8:59AM in Java 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.



  • gimbal2
    gimbal2 Member Posts: 11,949 Gold Trophy
    edited Dec 23, 2011 8:59AM
    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.
This discussion has been closed.