The moose likes EJB and other Java EE Technologies and the fly likes Threading in containers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Reply locked New topic

Threading in containers

Meghna Bhardwaj
Ranch Hand

Joined: Jun 08, 2007
Posts: 109

My question is about threading with respect to EJB and servlets. I have read conflicting facts about threading and servlets. Does the servlet container take care of threading? I think it does...however not too clear. If anyone has some good article links, it would be great.

I know that JSPs don't and if there is some code which could lead to threading issues then we should use the <%@ page isThreadSafe="false" %>.

Also what about EJB and threading does the application server take care of all that?? Does it create multiple threads for each client request received concurrently?

Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 443

You'll find us discussing this in the threading forum ;-) if you want to cross reference

Basically EJB's should be single threaded and your application becomes multi threaded in that your container / server will create \ manage threads as appropriate . In other parts of the container (non EJB's) is can be more of a guideline than a rule i.e. some people do create threads but generally there is an alternative J2EE mechanism to get the server to do the threading for you but it might be missing from a given server or the overhead of the mechanism considered to much to pay. The J2EE containers I have seen usually have some documentation advising you against explicitly creating threads in general (i.e. use an alternative J2EE mechanism and let the container handle the thread allocation) but thats not to say you might not be better of tackling a problem that way ;-)

"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Rejeev Divakaran

Joined: Aug 12, 2007
Posts: 27
I think her question is about 'thread safety', Is it Meghna?
EJBs are by definition thread safe.
However JSPs and Servlets are in general not thread safe. You can make them thread safe by specifying the certain parameters (as you mentioned). However making JSPs and Servlets thread safe is not a good design practice. It could lead access to the JSP/Servlet serializable (one request at a time). It depends on the number of JSP/Servlet instances available in the container. In most of the containers JSP/Servlet pool can be configurable.

Rejeev Divakaran,

Regards,<br />Rejeev<br />Blog: <a href="" target="_blank" rel="nofollow"></a>
Meghna Bhardwaj
Ranch Hand

Joined: Jun 08, 2007
Posts: 109
Thank you all for the responses. I have posted this topic in teh correct forum:

Yes, I am concerned about thread-safety as provided by the container, and when does the developer need to create threads themselves and take care of threading.

So is it correct to say that EJBs should be coded for a single thread and the container will manage/create multiple threads as required?

For the JSP and servlets is the container not managing threading or does it depend? For eg. in Tomcat servlet engine I have read that once you specify the page directive with isThreadSafe attribute the container will create N instances of the servlet depending on the number of concurrent requests coming in? So in this context it appears that Tomcat is creating/managing threads?

Would appreciate some responses, specially with respect to Tomcat.
[ October 18, 2007: Message edited by: Meghna Bhardwaj ]
I agree. Here's the link:
subject: Threading in containers
It's not a secret anymore!