Do I have to wait for my thread to finish?

M

mike

I am trying to spawn a new process in a servlet and I need that
process to continue running when the user is forwarded to another
page. I thought I could use threads, but I still have to wait for my
new thread to finish before my class finishes running(I am assuming
the garbage collector is waiting for my thread to finish before the
class exits.

Question 1: Is there a way to start the thread and then abandon it so
it still runs but my GC is free to clean it up?

Question 2: If there is no way to do that, how would you solve a
problem of a web application that is timing out while waiting for a
long process to finish(20-30 minutes)?
 
D

David Hilsee

mike said:
I am trying to spawn a new process in a servlet and I need that
process to continue running when the user is forwarded to another
page. I thought I could use threads, but I still have to wait for my
new thread to finish before my class finishes running(I am assuming
the garbage collector is waiting for my thread to finish before the
class exits.

Question 1: Is there a way to start the thread and then abandon it so
it still runs but my GC is free to clean it up?

Question 2: If there is no way to do that, how would you solve a
problem of a web application that is timing out while waiting for a
long process to finish(20-30 minutes)?

20-30 minutes is a long time to wait for something. Can you write the
results to some persistent store and let the user come back to check up on
it or retrieve the results? You could even send them an e-mail when it's
done.
 
O

Oscar kind

mike said:
I am trying to spawn a new process in a servlet and I need that
process to continue running when the user is forwarded to another
page. I thought I could use threads, but I still have to wait for my
new thread to finish before my class finishes running(I am assuming
the garbage collector is waiting for my thread to finish before the
class exits.

Question 1: Is there a way to start the thread and then abandon it so
it still runs but my GC is free to clean it up?

The JVM keeps references to all running threads, so they'll never be
collected. Finished threads are of course a different matter.

Question 2: If there is no way to do that, how would you solve a
problem of a web application that is timing out while waiting for a
long process to finish(20-30 minutes)?

I'd start a thread that reports of its progress in a persistent structure
that can be queried. Or I'd let the thread report its exit status via
email.
 
M

mike

Sending an e-mail is exactly what I want to do. I guess I must be
extremely bad at explaining my situation because no one ever
understands what I am talking about on here :eek:|

Because the process is so long I want to spawn a new thread and leave
it running while I go on to complete other tasks. I am starting my
thread, but the class that starts the thread finishes long before the
new thread is finished. The problem I am running into is that the
parent thread is waiting for the child thread to finish before it
returns to the class that called it.
Here's some pseudo code with one correction, the calling class is a
struts action class, not a servlet... would that make a difference?

<form submission calls action>

in the struts action:
//the long running process that implements Runnable.
LongProcess proc = new LongProcess()
Thread t1 = new Thread(proc);
t1.start();

return mapping.findForward("success");

<success mapping returns user to form>

So what is happening is that instead of starting the child thread and
going on, the action class is waiting for the thread to finish before
returning the forward mapping. Is this because I am creating the
actual thread object? Should I just be using "new
Thread(proc).start()" and maybe putting that in the session?
 
D

David Hilsee

mike said:
Sending an e-mail is exactly what I want to do. I guess I must be
extremely bad at explaining my situation because no one ever
understands what I am talking about on here :eek:|

Because the process is so long I want to spawn a new thread and leave
it running while I go on to complete other tasks. I am starting my
thread, but the class that starts the thread finishes long before the
new thread is finished. The problem I am running into is that the
parent thread is waiting for the child thread to finish before it
returns to the class that called it.
Here's some pseudo code with one correction, the calling class is a
struts action class, not a servlet... would that make a difference?

<form submission calls action>

in the struts action:
//the long running process that implements Runnable.
LongProcess proc = new LongProcess()
Thread t1 = new Thread(proc);
t1.start();

return mapping.findForward("success");

<success mapping returns user to form>

So what is happening is that instead of starting the child thread and
going on, the action class is waiting for the thread to finish before
returning the forward mapping. Is this because I am creating the
actual thread object? Should I just be using "new
Thread(proc).start()" and maybe putting that in the session?

It sounds like LongProcess is doing the work in its constructor insead of in
the run() method.

Depending on the usage, you could wind up with a lot of threads and overload
the system. If there were a lot of users, you instead use messaging and an
MDB, or a poor man's version of it by writing the request to a persistent
store that is like a queue and have a background process handle them
serially. Just an idea.
 
M

mike

Thanks for the help guys. The problem was that I was creating a
thread object instead of just calling "new
Thread(LongProcess).start()". With the object referring to the
thread, the class had to wait for the thread to complete so gc could
do it's work. The start() method returns void so there is nothing to
clean up and my Struts action continues on without a hitch.
 
J

John C. Bollinger

mike said:
Thanks for the help guys. The problem was that I was creating a
thread object instead of just calling "new
Thread(LongProcess).start()". With the object referring to the
thread, the class had to wait for the thread to complete so gc could
do it's work. The start() method returns void so there is nothing to
clean up and my Struts action continues on without a hitch.

I'm glad you have solved your problem. I'm sorry that your explanation
makes no sense at all. In particular:

(1) "new Thread(proc).start()" or "new Thread(new
LongProcess()).start()" does create a Thread object, whether or not you
store a reference to it.

(2) There is no way that the contents of a method's local variables, of
the corresponding instance's instance variables, or of any applicable
classes' static variables will prevent that method from returning once
it reaches a normal or abnormal exit point. At least, not in a
compliant JVM.

(3) Java GC does not hang unless the VM is _very_ buggy. The presence
of an object that cannot be GC'd simply means that fewer objects are
GC'd during that cycle, which is absolutely commonplace. GC may
occasionally take enough time to be noticeable, but typically only when
the VM is very low on memory. If that does happen then GC can start
taking the majority of your runtime, but usually that's a prelude to an
OutOfMemoryError.


What really happened, I don't know. Again, glad you solved the problem.


John Bollinger
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top