Randy Belknap wrote:

> >* Does the log() implementation write directly to an underlying file,
> >  or does it just queue messages to an in-memory buffer that is
> >  flushed asynchronously by a background thread?.
> >
>
> Craig,
>
> I've got an application that has to log some high level events (log in, document 
>views, etc.)  My appLog() method currently inserts these events directly into the 
>Oracle database and is unsynchronized.  I've been thinking about replacing this with 
>a system like you describe above, but am not sure about the 
>synchronization/performance issues.  Do you have some recommendations?
>
> What I was thinking was to have the appLog() method add the events into a List (I'm 
>using 1.1 collections) and then a timed process drain the List and write the events 
>to the database.
>
> If I did it this way, would I have to synchronize adding elements to the List?  
>Should the background process lock the List and copy it to a new List for processing?
>
>    List tempList = listOfEvents;  //listOfEvents is List used to log events
>    listOfEvents = new ArrayList();
>
> Thanks,
>
> Randy
>

When using the Collections API, synchronization is definitely something needed.  I 
would code the event enqueuer like this:

    List eventList = new ArrayLIst();  // Or whatever list type you want

    synchronized (tempList) {
        tempList.add(event);
    }

For the dequeuer thread, lock-and-copy seems like a reasonable way to go.  Then, you 
can iterate through the copy without interference by threads queueing new events 
during your output processing.  I don't have the collections API stuff right at hand, 
but it might have a clone() method you can use, instead of having to do it in a loop.

An alternative implementation would be to just dequeue one element at a time 
(synchronized) and remove it from the list, then do the output.  You'd need to be 
careful which list implementation you chose, though.

Craig McClanahan

___________________________________________________________________________
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

Reply via email to