This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes is this correct? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "is this correct?" Watch "is this correct?" New topic
Author

is this correct?

Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi all
again i'm back w/ a question and i wish it doesnt get un-answered as my rescent posts on JR
okay,
i have a requirement that forces me to do some background processing in servlet. the problem is - if my servlet takes longer to respond then the browser proxy times out (u'd know if u saw my post earlier describing the issue on this forum which is still to b responded by somebody). I started with using Runtime.exec() but the problem is if i don't read back the output or error from the Process then the process sleeps and never gets awakened so my efforts failed.
then i tried w/ threads from the servlet and here is the thing i do. now, guide me if u find anything wrong in it or a serious possible problem in it,
(tho this is pseudocode)

it seems to be working okay but i just wanted to get some reviews from all because i dont have much experience in this kind of development..

regards
maulin
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

If you feel you're having trouble getting your questions answered, try this article: How To Ask Questions The Smart Way.
Please use a meaningful topic name, and avoid contractions (u, ur, w/, u'd etc)
Dave
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

If I'm not mistaken, you're spawning a separate thread to do your processing, and you don't need the results of this thread for your Servlet.
Why is the code and management in the Servlet? I can't see why it's the Servlet's responsibility. There isn't any real difference between this and your description of your first solution. You are relying on the destroy method to make sure your hanging threads finish, but there is no guarantee of when this will be called and it won't force the Threads to finish if they haven't already.
Do you need the extra clean-up code? Can you just fire and forget?
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi David
thanks for your suggestion. i would try to read the link you provided and see how can i improve on things. yeah i agree, i'm a bad presenter i guess its time to cut off on these chatting arena short forms
anyways,
well, the thread code is in the separate file and i pass appropriate arguments to the thread when i create one so i guess its not in the servlet if thats what you meant.
the problem in the first approach where i use Runtime.exec() was that the process that was getting forked was going into sleep mode after sometime and never was getting awakened and my application was getting screwed up.
When we use Runtime.exec() then we have to read in the output from the process (or error stream) to make sure it was completed. Also we should call process.waitFor() may be. Now, if i have to do that then the problem of browser proxy timeout can't be solved as i would wait for response from the process forked by Runtime.exec() which takes time. So, i basically fork parallel thread and respond to the user that they are done. well, this is kind of cheating but it is better than getting headaches in production system as a result of browser proxy timeout.
Hope I am able to explain my position here.
About the cleanup code you asked me, I guess I need cleanup code because otherwise if the servlet gets unloaded by the webserver somehow then the threads will stray around and all you know. So, to avoid that I am putting all the forked thread in a list and then on destroy() I am making sure every thread on the list was completed. Well, this is what I remember from Oreilly book or something that cautioned me about using threads in servlet as the servlet is also managed by threads by the webserver.
Also, I am scared as usually people recommend not to spawn a thread in a servlet. Though I have not any substantial argument to support my fear.
Also, please forgive me for still lower case letters at many places instead of upper case but you know it will take couple of posts before I learn. I was there in some earlier thread discussing the weirdness with which people write posts and I guess this is my last post where you would find weiredness in writing.
Regards
Maulin.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

So the real problem is that the thread created to run Runtime.exec() is not completing?
Does the MyThread.run() method still call Runtime.exec(), or does it do the work itself? Either way, it's probably time to get this thread moved to the Threads forum.
Dave
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi David
No. In the thread I don't use Runtime.exec().
Also, I am sure about that Runtime.exec() issue as we have to read in back the output/error from the stream as otherwise it doesn't get completed sometimes but I just wanted to try and it really worked for small processing but for big it failed
Regards
Maulin
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: is this correct?
 
Similar Threads
directory structure questions...
join a thread behind that thread...
Memory Leak
how to kill or stop a thread in java2
Inner Class Definition