Nic Ferrier wrote:
>
> > Wai Mun <[EMAIL PROTECTED]> 02-Jun-01 3:26:00 PM >>>
>
> >how do i update the counter file efficiently?
>
> The constructor:
> - creates a seperate thread for managing the file
>
If you create a thread in the init() method, you have
to get rid of it in the destroy() method. But Java
doesn't make it easy to get rid of threads: the thead
itself has to cooperate, and be very carefully written.
So if at all possible it's a good idea to avoid spawning
threads in servlets.
It might seem trivial (what's a couple of extra threads
laying around?), but it's like not free'ing memory in
C++: fine for little 5 line utilities, but in servers,
leaks accumulate and are expensive to hunt down. Plus,
it's just bad practice.
Depending on the expected load, it might make sense to
just try open/write/close-on-every-request and see
if it really is a problem. Premature optimization is the
root of all evil, etc.
If that doesn't work, then Nic's idea works fine without
using a thread if you have the log method itself write out
the buffer once every 1000 (or 100 or 10 or whatever)
requests. That way, the file i/o cost is amortized out over
many requests. But remember to flush the buffer in the
destroy() method of the servlet. And you might add a way
for a servlet request to force a buffer flush.
--
Christopher St. John [EMAIL PROTECTED]
DistribuTopia http://www.distributopia.com
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html