Do I have to wait for my thread to finish?

Discussion in 'Java' started by mike, Aug 6, 2004.

  1. mike

    mike Guest

    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)?
     
    mike, Aug 6, 2004
    #1
    1. Advertising

  2. mike

    David Hilsee Guest

    "mike" <> wrote in message
    news:...
    > 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
     
    David Hilsee, Aug 6, 2004
    #2
    1. Advertising

  3. mike

    Oscar kind Guest

    mike <> 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
     
    Oscar kind, Aug 7, 2004
    #3
  4. mike

    mike Guest

    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?

    "David Hilsee" <> wrote in message news:<>...
    > "mike" <> wrote in message
    > news:...
    > > 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.
     
    mike, Aug 7, 2004
    #4
  5. mike

    David Hilsee Guest

    "mike" <> wrote in message
    news:...
    > 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.

    --
    David Hilsee
     
    David Hilsee, Aug 7, 2004
    #5
  6. mike

    mike Guest

    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" <> wrote in message news:<>...
    > "mike" <> wrote in message
    > news:...
    > > 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.
     
    mike, Aug 9, 2004
    #6
  7. 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
     
    John C. Bollinger, Aug 9, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gary J
    Replies:
    19
    Views:
    39,280
    Chris Uppal
    Aug 31, 2004
  2. Replies:
    8
    Views:
    4,546
  3. Uros
    Replies:
    2
    Views:
    334
  4. Christopher Dancy
    Replies:
    4
    Views:
    112
    Roger Pack
    Apr 16, 2010
  5. Zhidian Du
    Replies:
    2
    Views:
    110
    David Efflandt
    Feb 21, 2004
Loading...

Share This Page