5

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.

saplingPro
  • 20,769
  • 53
  • 137
  • 195

3 Answers3

18

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

Community
  • 1
  • 1
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • ... bad idea unless you need to do some long-running CPU-intensive task. – Petr Gladkikh Apr 26 '12 at 09:02
  • @Tomasz Nurkiewicz even if i keep the new thread's work inside the `doPost()` method of that servlet? – saplingPro Apr 26 '12 at 09:06
  • @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? – Suhail Gupta Apr 26 '12 at 09:11
  • 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?). – BigMike Apr 26 '12 at 09:14
  • 1
    @grassPro: if your thread runs inside `doPost()` (you are waiting for the thread result using `join()`?) what's the point of thread? – Tomasz Nurkiewicz Apr 26 '12 at 09:20
  • @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 Apr 26 '12 at 09:20
  • @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 – Tomasz Nurkiewicz Apr 26 '12 at 09:21
  • @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. – BigMike Apr 26 '12 at 09:23
-2

Servlets are implicitly run in new threads by webserver, so whenever any request hits a servlet , it will be executed in a different thread. i dont foresee a reason to create a fresh thread yourself

Akash Yadav
  • 2,411
  • 20
  • 32
  • maybe just not to keep the client freezed (if the thread is time consuming?) – BigMike Apr 26 '12 at 08:56
  • So the user does not have to wait for servlet to finish upload – Piotr Kochański Apr 26 '12 at 08:57
  • @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. – Akash Yadav Apr 26 '12 at 08:58
-5

There is nothing wrong in starting new thread in Servlet (unlike EJB), so yes, it is ok.

EDIT: second thought @Tomasz Nurkiewicz is right. The file upload will be stopped.

Piotr Kochański
  • 21,862
  • 7
  • 70
  • 77