File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes servlet container and multi threading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet container and multi threading" Watch "servlet container and multi threading" New topic
Author

servlet container and multi threading

Kev lam
Greenhorn

Joined: Mar 04, 2005
Posts: 14
My colleague and I had a discussion about using multi-threading in an internet application. I think that the servlet container has its own thread pool to handle the requests. If using multi-threading, the container will get confused and will generate unprdictable results. Could anyone tell me whether I'm right or not.

P.S. if the admin thinks this question belongs to Thread forum, please feel free to help me move there.

Thanks.
Kevin
Ed Wallen
Ranch Hand

Joined: Feb 11, 2002
Posts: 34
Servlets are by default multi-threaded unless you specifically implement javax.servlet.SingleThreadModel.
Kev lam
Greenhorn

Joined: Mar 04, 2005
Posts: 14
Ed,

Thank you for your reply and sorry I did not make it clear.

I mean I create a thread pool and maintain it myself. The threads will be used to do some stuff like calling a web service and doing some calculation.
Is there a conflict between the thread pool maintained by Servlet Container and the thread pool I created. I just can not see how it works.

Thanks.
Kevin
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Ed Wallen:
Servlets are by default multi-threaded unless you specifically implement javax.servlet.SingleThreadModel.


It is deprecated, long time ago.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Kevin Cui:
I mean I create a thread pool and maintain it myself. The threads will be used to do some stuff like calling a web service and doing some calculation.
Is there a conflict between the thread pool maintained by Servlet Container and the thread pool I created. I just can not see how it works.


Yes you can do that. But should be outside your servlet, otherwise all messed up. A servlet should just entertain your request and give response.
Kev lam
Greenhorn

Joined: Mar 04, 2005
Posts: 14
Adeel,

Thanks for your reply.

(1)If I have a class called ThreadsManager and all threads related activities such as creating threads, starting threads, and terminating threads are handled by this class. In my servlet, I call ThreadsManager.startManageThreads(). Can this consider as "outside the servlet"? If not, could you give me an example?

(2)Could you explain a little bit more on "other all messed up"?

Thanks.
Kevin
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Originally posted by Kevin Cui:
(2)Could you explain a little bit more on "other all messed up"?


Consider a situation where (for some reason) you give multiple threads access to the output stream and get them to write to the client. Without some management the client is going to get strange results. Some would call it garbage.

This sounds unreasonable, but you can create this situation when attempting to include JSPs from another context. Depending on the vendor, the request may re-enter through the thread pool, get assigned a different thread, and you have no control over where the included content actually gets placed. This is also one of my favourite examples for talking people out of trying to be clever with cross-context responsibilities
Kev lam
Greenhorn

Joined: Mar 04, 2005
Posts: 14
Thanks David. I think I got your point.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: servlet container and multi threading