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 fork a thread from a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "fork a thread from a servlet" Watch "fork a thread from a servlet" New topic
Author

fork a thread from a servlet

Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Hi all,
Why it is not recommended to fork a thread from a servlet?
Thanks!
Maulin
Tim Baker
Ranch Hand

Joined: Oct 04, 2003
Posts: 541
you got me i do it all the time.
i suppose if your talking about something not closely related to the servlet you don't wanna do it because the server may go down, or be restarted, and that will affect your thread. i suppose it adds load on to the server as well. there aren't that many things that you would want to do this with, and when ou dont need to youd be better off creating a seperate application.


Kim Jong II (North Korea's Dear Leader) said:Nuclear weapons don't kill people, people kill people.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Hi Tim,
Thanks for the response. I would appreciate if Marty can try to explain this in more details.
Its not that I have not used this or I am new to JSP/servlets but I am just wondering "why" about this because I am not able to convince myself.
What I believe is,
if we fork threads from a servlet and the servlet dies then those threads would become "orphaned" (zombie processes in Unix), right? and I am not sure if they would even be completed or not. If those thread doesn't get completed and not even get out of machines' process list then it will be a problem, right?
Please throw more light on this.
Regards
Maulin
Marty Hall
Author
Ranch Hand

Joined: Jan 02, 2003
Posts: 111
Why it is not recommended to fork a thread from a servlet?

Who says it is not recommended? I do this moderately often. Some things to keep in mind:
  • The system already spawns a new thread for each request. So you do not need to start your own thread unless you want some process to continue after the response is finished.
  • If you keep a thread running after the response finishes, you need some way to get that data to the user later. The most common solutions are session tracking (the same user sees updates later when they return) or the Refresh header (you tell the client to come back in 5 seconds for an update).
  • You usually want to set the priorities of your own threads to a low value. That way, the threads that keep running after the response finishes do not slow down the server.
  • You do not have to worry about zombie processes. Threads are lightweight, unlike the heavyweight processes that "fork" yields, and the server will automatically garbage collect threads whose "run" methods have finished executing.


  • Cheers-
    - Marty


    Java training and consulting
    Marty Hall
    Author
    Ranch Hand

    Joined: Jan 02, 2003
    Posts: 111
    PS There is an example (and discussion) of starting your own threads and using the Refresh header in Section 7.4 of the second edition of Core Servlets and JSP.
    Maulin Vasavada
    Ranch Hand

    Joined: Nov 04, 2001
    Posts: 1871
    Thanks Marty Hall.
    I feel better
    I did take care of threads in my servlet via keeping Vector of the thread references I create on doGet request and then making sure that I wait for those threads in destroy() method via join() call...but from your description it seems that it would have been okay even if I didn't do it that way..
    so I had sth like...
    public myservlet extends HttpServlet {
    Vector myThreads = new Vector();
    // please ignore language semantics for easiness
    doGet() {
    Thread t = new Thread();
    myThreads.add(t);
    t.start();
    out.println("You are done.");
    }
    destroy() {
    for(int i=0; i < myThreads.size(); i++) {
    Thread t = (Thread)myThreads.elementAt(i);
    t.join(); // assume other requirements of catching exceptions etc are satisfied..
    }
    }
    }

    But now I will keep this posting on my bookmark
    Regards
    Maulin
    Marty Hall
    Author
    Ranch Hand

    Joined: Jan 02, 2003
    Posts: 111
    I did take care of threads in my servlet via keeping Vector of the thread references I create on doGet request and then making sure that I wait for those threads in destroy() method via join() call...but from your description it seems that it would have been okay even if I didn't do it that way.

    Right. You didn't need to do that. Don't forget to lower the priority of your custom threads, though.
    Cheers-
    - Marty
    Juan Rolando Prieur-Reza
    Ranch Hand

    Joined: Jun 20, 2003
    Posts: 236
    Originally posted by Marty Hall:
    ...the server will automatically garbage collect threads whose "run" methods have finished executing...

    Ah ha! This is the reason why I (also) thought that we cannot effectively start threads which must still be running when the client eventually sends a new request - not to mention how the heck to associate the new request with the "dangling" thread from the previous (stateless) thread. I'll have to look into this more when I have a chance to play with JSP more.


    Juan Rolando Prieur-Reza, M.S., LSSBB, SCEA, SCBCD, SCWCD, SCJP/1.6, IBM OOAD, SCSA
    Ko Ko Naing
    Ranch Hand

    Joined: Jun 08, 2002
    Posts: 3178
    Originally posted by john prieur:

    I'll have to look into this more when I have a chance to play with JSP more.

    Hi John,
    Usually threads are created and used in servlets only, since JSP is the recommended component to be used as a presentation part to the client... I guess you probably overlooked the word that you use... Just wanna correct some neglectable mistake...


    Co-author of SCMAD Exam Guide, Author of JMADPlus
    SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
    Juan Rolando Prieur-Reza
    Ranch Hand

    Joined: Jun 20, 2003
    Posts: 236
    Originally posted by Ko Ko Naing:
    guess you probably overlooked the word that you use...

    No, I did not think (or say) that the HTML resulting from JSP somehow spawns threads. Thanks for the comment anyway. To clarify my comment, I think the difficult issue is how to associate a subsequent request from the client with a thread started by the servlet (either an explicit servlet or one generated because of a JSP). I am just wondering how the session known to the servlet can be used by the descendant thread (especially if the originating servlet went away).
    Cheers!
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: fork a thread from a servlet