Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Do I have to wait for my thread to finish?

Reply
Thread Tools

Do I have to wait for my thread to finish?

 
 
mike
Guest
Posts: n/a
 
      08-06-2004
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)?
 
Reply With Quote
 
 
 
 
David Hilsee
Guest
Posts: n/a
 
      08-06-2004
"mike" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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.

--
David Hilsee


 
Reply With Quote
 
 
 
 
Oscar kind
Guest
Posts: n/a
 
      08-07-2004
mike <(E-Mail Removed)> wrote:
> 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.


--
Oscar Kind http://home.hccnet.nl/okind/
Software Developer for contact information, see website

PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
 
Reply With Quote
 
mike
Guest
Posts: n/a
 
      08-07-2004
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 |

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?

"David Hilsee" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> "mike" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> > 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.

 
Reply With Quote
 
David Hilsee
Guest
Posts: n/a
 
      08-07-2004
"mike" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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 |
>
> 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.

--
David Hilsee


 
Reply With Quote
 
mike
Guest
Posts: n/a
 
      08-09-2004
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.


"David Hilsee" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> "mike" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> > 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 |
> >
> > 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.

 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      08-09-2004
mike wrote:

> 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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to wait() Thread in EJB code _ Java 2 10-12-2005 07:40 PM
Thread and wait simple question hilz Java 6 06-07-2005 05:56 AM
How to make "fork/wait" to WAIT longer? Huey C Programming 1 03-01-2004 02:01 PM
java Bug? -> Java threads: Wait X Notify X [Thread AGAINST Runnable] Renato Battaglia Java 3 10-24-2003 04:54 PM
j2me thread wait/notify question Stee1HeD Java 3 08-12-2003 12:03 AM



Advertisments