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.
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.