starting a new thread in servlet

12,214

It is not only a bad idea, but it also won't work. Here is why: your file upload request will eventually hit doPost() method. As long as you are in this method, the container keeps the connection open. Once you return from that method (and if you decide to handle incoming data in a separate thread, doPost() will finish early) the container assumes you are done with the request and will close the connection. From the client perspective the upload was interrupted by the server. And because of the asynchronous nature of threads the interruption will occur in random moment.

Believe me, some users already experienced that: HttpServletResponse seems to periodically send prematurely.

Moreover it is a bad idea to start new thread per request as this scales poorly (and it is even prohibited by some specifications). What you can do is to use Servlet 3.0 asynchronous request and handle uploads asynchronously, but preferably using some pool of threads. See also: Why create new thread with startAsync instead of doing work in servlet thread?.

Share:
12,214
saplingPro
Author by

saplingPro

I love to write code and love to be known as a programmer. I try very hard to be creative. I am a hard working man ! I don't know where i am heading and don't know where i will be :(

Updated on June 11, 2022

Comments

  • saplingPro
    saplingPro almost 2 years

    When a request reaches a servlet that handles uploading of files,is it a good idea to start a new thread in that servlet using new Thread(r).start() that will handle another piece of data that came with the file that was uploaded. I wanted to this to handle both the jobs parallely.

  • Buhake Sindi
    Buhake Sindi about 12 years
    Not true, writing a thread in a concurrent environment needs to be managed.
  • BigMike
    BigMike about 12 years
    maybe just not to keep the client freezed (if the thread is time consuming?)
  • Piotr Kochański
    Piotr Kochański about 12 years
    So the user does not have to wait for servlet to finish upload
  • Piotr Kochański
    Piotr Kochański about 12 years
    As long as thread is not using any non-local variables (fields in servlet) there is no problem. I assume everything happens inside doGet/doPost method
  • Akash Yadav
    Akash Yadav about 12 years
    @BigMike But usually, we dont give a servlet multiple responsibilities and if this servlet is handling the file upload there is nothing much you can do about it, So instead of create new thread i would say initiate a fresh request altogether from client side.
  • Petr Gladkikh
    Petr Gladkikh about 12 years
    ... bad idea unless you need to do some long-running CPU-intensive task.
  • saplingPro
    saplingPro about 12 years
    @Tomasz Nurkiewicz even if i keep the new thread's work inside the doPost() method of that servlet?
  • Suhail Gupta
    Suhail Gupta about 12 years
    @Tomasz Nurkiewicz will it be alright to dispatch a new request to another servlet from the run method of the thread ,inside post method of this servlet?
  • BigMike
    BigMike about 12 years
    You've triggered my curiosity :) I will try something with async stuffs, but I've got some bad feelings regarding the client side. (How to give back result of upload?).
  • Tomasz Nurkiewicz
    Tomasz Nurkiewicz about 12 years
    @grassPro: if your thread runs inside doPost() (you are waiting for the thread result using join()?) what's the point of thread?
  • Tomasz Nurkiewicz
    Tomasz Nurkiewicz about 12 years
    @SuhailGupta: yes, there is nothing wrong in calling another servlet (requesting another URL) from doPost() - as long as you wait for the response and do not recieve it in another thread)
  • Tomasz Nurkiewicz
    Tomasz Nurkiewicz about 12 years
    @BigMike: from the client perspective this is just a very long running connection, the client won't see the threading/asynchronous stuff going on inside server
  • BigMike
    BigMike about 12 years
    @TomaszNurkiewicz ok, I've just read how async works on servlet. Makes sense, it's a bit different from ASP.NET async controllers but looks interesting. Thanks also for the latest link added.