Hi Jeff,

The default behavior is that multiple threads owned by the servlet engine may be
running your servlet at any point in time. If you make it single threaded, the
servlet engine will typically create more than one instance of the servlet and
simply assure that only one thread is ever running any particular instance.

The code you gave as an example won't work, because the servlet engines assume
that when their thread returns from the call to your service method you've
finished your handling of the request. In the example, the servlet engine thread
returns after starting your new thread, so the response would be considered
complete at that time.

However, you can use the servlet engine thread to do whatever you want in
processing your request, as long as it doesn't return from the service method. A
fairly ugly (at least from the performance standpoint) approach would be to
modify the code in your example so that the servlet engine thread waits for a
completion signal from the spawned thread before returning from the doGet. A
somewhat cleaner approach would be to just have it call the run() method on your
objectToHandleRequest directly, without creating a new thread. There's no need
to even make this a Runnable, though, it can be any type of object with a method
for the thread to call in order to handle the request.

Hope this helps,

  - Dennis

Dennis M. Sosnoski
Sosnoski Software Solutions, Inc.
http://www.sosnoski.com

Jeff Borton wrote:
>
> Ok,
>         I've search the archive looking to see if anyone has "talked" about this
> before, but I didn't see any postings.  So I'm going to ask.  I'm developing
> a servlet that I want to have multi-threading support in.  I thought if I
> made the servlet multi-threaded the servlet engine would create a new
> thread/instance for each person "hitting" my servlet. But from some of the
> other posts I've seen this isn't the case, unless I use the single threaded
> model.  But from the same posts, it sounds like that is going to be a
> performance hit.  Question: will I get the same performance hit if I do the
> threading myself?  Example:
>
> void doGet(HttpServletRequest req, HttpServletResponse resp)
> {
>         Runnable objectToHandleRequest = new RunnableObjectToHandleRequest(req,
> resp);
>         Thread newThread = new Thread(objectToHandleRequest );
>         newThread.start();
> }
>
> In this way I'm sure I have all my variables and objects within one thread
> where I can control the accessing/changing data.  And I can have one
> database connection per thread.
>
> Is this a bad idea or a good idea?
>
> Thanks,
> Jeff
>
> ___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to