Hi,
Thread.join() can be very interesting and have non-obvious behavior in a servlet.

Here's a simple alternative design:

public class MyReportJob implements Runnable
{
  private boolean done = false;
  ...
  public void run()
  {
    ... do work ...
    done = true;
  }

  public boolean isDone()
  {
    return done;
  }
  
}

Then, in your servlet:

Runnable myReportJob = new MyReportJob();
Thread myReportThread = new Thread(myReportJob);
myReportThread.start();

while(! (myReportJob.isDone()))
{
  ... wait ...
}

And when you get here, you're done with the job and
can present its results.

So the question is, if you're waiting for a thread anyways, why not just do the work 
in the servlet itself and make the code simpler?  There are several advantages (but 
also at least one disadvantage) to the above:

- You can present a "please wait, I'm doing work" graphic or whatever to the user 
while the thread is running.

- You can place a time bound on the thread

- You can use a thread pool with pre-allocated report creation threads to improve 
performance

- You're separating the processing logic for report creation from the servlet or JSP 
presentation of the report results (to some extent)

Still, it may be better to go a little further and have a Singleton or Factory for 
reports.  You would just specify the params and let that singleton worry about thread 
creation etc.  This could be a useful Façade if you decide to eventually have the 
report creation done elsewhere (e.g. another server)....

Like most design questions, there are a lot of ways to go on this one ;)

Yoav Shapira
Millennium ChemInformatics


Yoav Shapira
Millennium ChemInformatics


>-----Original Message-----
>From: Phillip Rhodes [mailto:rhodespc@;telerama.com]
>Sent: Sunday, October 27, 2002 1:01 PM
>To: [EMAIL PROTECTED]
>Subject: start new thread from servlet?
>
>Sorry, I searched and searched for four hours all over the place and have
>not found a definitive how-to or example.
>Sorry if this is off-topic...
>
>In my servlet, I will be running long reports.  I want to kick off a new
>thread that will run the report.
>I read somewhere that I have to join my new thread to the thread of my
>servlet to avoid problems.
>
>Can anyone point me to a how-to on this?
>Thanks!
>
>Phillip
>
>
>--
>To unsubscribe, e-mail:   <mailto:tomcat-user-
>[EMAIL PROTECTED]>
>For additional commands, e-mail: <mailto:tomcat-user-
>[EMAIL PROTECTED]>

This e-mail, including any attachments, is a confidential business communication, and 
may contain information that is confidential, proprietary and/or privileged.  This 
e-mail is intended only for the individual(s) to whom it is addressed, and may not be 
saved, copied, printed, disclosed or used by anyone else.  If you are not the(an) 
intended recipient, please immediately delete this e-mail from your computer system 
and notify the sender.  Thank you.

--
To unsubscribe, e-mail:   <mailto:tomcat-user-unsubscribe@;jakarta.apache.org>
For additional commands, e-mail: <mailto:tomcat-user-help@;jakarta.apache.org>

Reply via email to