Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

fork a thread from a servlet

 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Why it is not recommended to fork a thread from a servlet?
Thanks!
Maulin
 
Tim Baker
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
     
    Marty Hall
    Author
    Ranch Hand
    Posts: 111
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 1873
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 111
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 237
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Ko Ko Naing
    Ranch Hand
    Posts: 3178
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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...
     
    Juan Rolando Prieur-Reza
    Ranch Hand
    Posts: 237
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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!
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic